Skip to content

Commit

Permalink
๐Ÿ”€ ๋นŒ๋“œ ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•œ ๋ธŒ๋žœ์น˜ ๋จธ์ง€
Browse files Browse the repository at this point in the history
dev/sh ๋ธŒ๋žœ์น˜์˜ ๊ฐ’์„ ๋จธ์ง€ํ•ฉ๋‹ˆ๋‹ค.
  • Loading branch information
Ganghee-Lee-0522 authored Jan 12, 2024
2 parents 7b7ceab + ac15c26 commit 947df18
Show file tree
Hide file tree
Showing 39 changed files with 439 additions and 224 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ jobs:

# Gradle๋กœ ๋นŒ๋“œ ์‹คํ–‰
- name: Build with Gradle
run: ./gradlew bootJar
run: |
chmod +x ./gradlew
./gradlew bootJar
# ๋นŒ๋“œ ๊ฒฐ๊ณผ Slack ์•Œ๋žŒ ์ „์†ก
- name: Send Slack Alarms
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.committers.snowflowerthon.committersserver.auth.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

import java.util.List;

@Configuration
public class CorsConfig {
@Bean
public CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration corsConfig = new CorsConfiguration();

corsConfig.setAllowCredentials(true);
corsConfig.setAllowedOrigins(List.of("https://kidari.site", "https://kidari-server.shop"));
corsConfig.setAllowedMethods(List.of("GET", "POST", "PATCH", "DELETE", "OPTIONS"));
corsConfig.setAllowedHeaders(List.of("*"));
corsConfig.setAllowedHeaders(List.of("*"));
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", corsConfig);

return source;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
Expand Down Expand Up @@ -47,26 +48,26 @@ public BCryptPasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}

@Bean
public CorsConfigurationSource corsConfigurationSource() {

CorsConfiguration config = new CorsConfiguration();
// config.setAllowedHeaders(Collections.singletonList("*"));
// config.setAllowedMethods(Collections.singletonList("*"));
// config.addAllowedOriginPattern("http://localhost:3000");
// config.addAllowedOriginPattern("https://kidari.site");
// config.addAllowedOriginPattern("https://kidari.site:3000");
// @Bean
// public CorsConfigurationSource corsConfigurationSource() {
//
// CorsConfiguration config = new CorsConfiguration();
//// config.setAllowedHeaders(Collections.singletonList("*"));
//// config.setAllowedMethods(Collections.singletonList("*"));
//// config.addAllowedOriginPattern("http://localhost:3000");
//// config.addAllowedOriginPattern("https://kidari.site");
//// config.addAllowedOriginPattern("https://kidari.site:3000");
//// config.setAllowCredentials(true);
//
// config.addAllowedOriginPattern("*"); // addAllowedOrigin("*")์€ allowCredentials(true)๋ž‘ ๊ฐ™์ด ์‚ฌ์šฉ ๋ถˆ๊ฐ€๋Šฅ
// config.addAllowedMethod("*");
// config.addAllowedHeader("*");
// config.setAllowCredentials(true);

config.addAllowedOriginPattern("*"); // addAllowedOrigin("*")์€ allowCredentials(true)๋ž‘ ๊ฐ™์ด ์‚ฌ์šฉ ๋ถˆ๊ฐ€๋Šฅ
config.addAllowedMethod("*");
config.addAllowedHeader("*");
config.setAllowCredentials(true);

UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", config);
return source;
}
//
// UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
// source.registerCorsConfiguration("/**", config);
// return source;
// }

@Bean
protected SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
Expand All @@ -78,6 +79,7 @@ protected SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
.authorizeHttpRequests((authorizeRequests) -> {
authorizeRequests
.requestMatchers(permitList).permitAll()
.requestMatchers(HttpMethod.OPTIONS, "/**").permitAll()
.anyRequest().hasAnyAuthority("ROLE_MEMBER");
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,13 @@ private int getCommits(String nickname, List<RepoResponseDto> repositories) {
List<CommitResponseDto> commits = gitHubClient.getCommit(repo.full_name, token);

for(CommitResponseDto com : commits) {
String committer = com.author.login;
if(committer.isEmpty() || committer.isBlank()) {
log.info("์ปค๋ฏธํ„ฐ ๋ถ€์žฌ");
continue;
}
log.info("์ปค๋ฐ‹ sha -> {}", com.sha);
log.info("์ปค๋ฏธํ„ฐ -> {}", com.author.login);
String committer = com.author.login;
if (committer.equals(nickname)) {
totalCommits++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public String getNicknameFromToken(String token) {
}

public Long getMemberIdFromToken(String token) {
return (Long) getClaims(token).get("memberId");
return Long.valueOf(getClaims(token).get("memberId").toString());
}

public Role getRoleFromToken(String token) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ public enum ErrorCode {

// Univ
UNIV_NOT_FOUND(404, "์กด์žฌํ•˜์ง€ ์•Š๋Š” ๋Œ€ํ•™์ž…๋‹ˆ๋‹ค."),
UNIV_REGISTER_BAD_REQUEST(404, "๋Œ€ํ•™๊ต ๋“ฑ๋ก/์ทจ์†Œ ์ž˜๋ชป๋œ ์š”์ฒญ์ž…๋‹ˆ๋‹ค."),
UNIV_CANNOT_BE_REGISTERED(404, "ํ˜„์žฌ ๋“ฑ๋กํ•œ ๋Œ€ํ•™์ด ์žˆ์–ด ์ƒˆ๋กœ์šด ๋Œ€ํ•™์„ ๋“ฑ๋กํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."),

// Snowflake๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Œ
SNOWFLAKE_CANNOT_BE_USED(404, "ํ˜„์žฌ ๋ณด์œ  ์ค‘์ธ ๋ˆˆ์†ก์ด๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค."),

;

private final int statusCode;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package com.committers.snowflowerthon.committersserver.common.validation;

import com.committers.snowflowerthon.committersserver.common.response.exception.*;
import com.committers.snowflowerthon.committersserver.domain.item.entity.Item;
import com.committers.snowflowerthon.committersserver.domain.item.entity.ItemRepository;
import com.committers.snowflowerthon.committersserver.domain.member.entity.Member;
import com.committers.snowflowerthon.committersserver.domain.member.entity.MemberRepository;
import com.committers.snowflowerthon.committersserver.domain.univ.entity.Univ;
import com.committers.snowflowerthon.committersserver.domain.univ.entity.UnivRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@RequiredArgsConstructor
@Service
public class ValidationService {

private final MemberRepository memberRepository;
private final ItemRepository itemRepository;
private final UnivRepository univRepository;

public Member valMember(String nickname) {
Expand All @@ -26,13 +27,18 @@ public Member valMember(Long memberId) {
return memberRepository.findById(memberId)
.orElseThrow(() -> new MemberException(ErrorCode.MEMBER_NOT_FOUND));
}
// ๋žญํ‚น์—์„œ ๋ฆฌ์ŠคํŠธ ๋ฐ˜ํ™˜ ์œ„ํ•ด
/*
public List<Member> valMemberList(String nickname) {
List<Member> memberList = memberRepository.findAllByNickname(nickname);
if (memberList == null || memberList.isEmpty()) {
throw new MemberException(ErrorCode.MEMBER_NOT_FOUND);
}
return memberList;
}*/

public Item valItem(Long itemId) {
return itemRepository.findById(itemId)
.orElseThrow(() -> new ItemException(ErrorCode.ITEM_NOT_FOUND));
}

public Univ valUniv(Long univId) {
Expand All @@ -44,4 +50,5 @@ public Univ valUniv(String univName) { // ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค์ง€ ์•Š์Œ์—
return univRepository.findByUnivName(univName)
.orElse(null);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.committers.snowflowerthon.committersserver.domain.attack.controller;

import com.committers.snowflowerthon.committersserver.common.response.ApiResponse;
import com.committers.snowflowerthon.committersserver.common.response.exception.ErrorCode;
import com.committers.snowflowerthon.committersserver.domain.attack.dto.AttackDto;
import com.committers.snowflowerthon.committersserver.domain.attack.service.AttackService;
import lombok.RequiredArgsConstructor;
Expand All @@ -11,23 +13,23 @@

@RestController
@RequiredArgsConstructor
@CrossOrigin(origins = "https://kidari.site")
@CrossOrigin(origins = "https://kidari.site", allowedHeaders = "*")
@RequestMapping("/api/v1")
public class AttackController {

public final AttackService attackService;

@GetMapping("/home/alarm")
public ResponseEntity<List<AttackDto>> getAlarms() {
List<AttackDto> alarmList = attackService.findAlarms();
return ResponseEntity.ok().body(alarmList); // null์ผ ์ˆ˜ ์žˆ์Œ
@GetMapping("/home/alarm") // ์•Œ๋ฆผ ์กฐํšŒ (๊ณต๊ฒฉ๋ฐ›์€ ๋ชฉ๋ก ์กฐํšŒ)
public ResponseEntity<?> getAlarms() {
List<AttackDto> attackedList = attackService.findAlarms();
return ResponseEntity.ok().body(ApiResponse.success(attackedList)); // null์ผ ์ˆ˜ ์žˆ์Œ
}

@PatchMapping("/user/attack")
public ResponseEntity<?> attackUser (@RequestParam String nickname) {
public ResponseEntity<?> attackUser(@RequestParam String nickname) {
if (attackService.tryAttack(nickname)) {
return ResponseEntity.ok(HttpStatus.CREATED);
return ResponseEntity.ok().body(ApiResponse.success()); // ๊ณต๊ฒฉ ์„ฑ๊ณต
}
return ResponseEntity.badRequest().build();
return ResponseEntity.badRequest().body(ApiResponse.failure(ErrorCode.SNOWFLAKE_CANNOT_BE_USED)); // ๋ˆˆ์†ก์ด๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Builder
@Getter
@AllArgsConstructor
@NoArgsConstructor
public class AttackDto {
private String time;
private String attacker;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.committers.snowflowerthon.committersserver.domain.member.entity.MemberRepository;
import com.committers.snowflowerthon.committersserver.domain.member.service.MemberService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
Expand All @@ -16,69 +17,74 @@
import java.util.Comparator;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class AttackService {
private final ValidationService validationService;
private final AttackRepository attackRepository;
private final MemberRepository memberRepository;
private final ValidationService validationService;
private final MemberService memberService;

// AttackController์˜ getAlarms ๊ด€๋ จ

public List<AttackDto> findAlarms() {
Member member = memberService.getAuthMember();
member.alarmChecked();
member.alarmChecked(); // ๋‚ด ์•Œ๋ฆผ์„ ํ™•์ธ๋œ ์ƒํƒœ๋กœ ๋ณ€๊ฒฝ
memberRepository.save(member);

List<Attack> attackedList = attackRepository.findAllByMember(member); // ๋‚ด๊ฐ€ ๊ณต๊ฒฉ๋ฐ›์€ ๋ชฉ๋ก ๊ฐ€์ ธ์˜ค๊ธฐ
if (attackedList == null || attackedList.isEmpty()) //๊ณต๊ฒฉ๋ฐ›์€ ๊ธฐ๋ก์ด ์—†๋Š” ๊ฒฝ์šฐ
return null;
attackedList.sort(Comparator.comparing(Attack::getTime).reversed()); // ์‹œ๊ฐ„์„ ์ตœ์‹ ์ˆœ์œผ๋กœ ์ •๋ ฌ
List<AttackDto> attackDtoList = new ArrayList<>();

List<AttackDto> attackDtoList = new ArrayList<>(); // ์‘๋‹ต ํ˜•์‹ ๋ฐ˜ํ™˜ ๋ฐ ๊ฐœ๋ณ„ ๊ณต๊ฒฉ ํ™•์ธ ์œ„ํ•ด AttackDto ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ
for (Attack attack : attackedList) {
AttackDto attackDto = formatAttackDto(attack);
attackDtoList.add(attackDto);
if (!attack.getIsChecked()) {
attack.checkAttack(); // ๊ฐœ๋ณ„ atk์˜ isChecked ๋ฅผ true ๋กœ ์„ค์ •
attackDtoList.add(formatToAttackDto(attack)); // ํ˜•์‹ ๋ณ€ํ™˜ํ•ด ๋ฆฌ์ŠคํŠธ์— ๋„ฃ์Œ
if (!attack.getIsChecked()) { // isChecked๊ฐ€ true: ์ด๋ฏธ ์ด์ „์— ํ™•์ธ ์™„๋ฃŒ, false: ์•„์ง ์•ˆ ํ™•์ธ(์ด๋ฒˆ์— ํ™•์ธ)
attack.checkAttack();
}
attackRepository.save(attack); // ์ €์žฅ์†Œ ์—…๋ฐ์ดํŠธ
attackRepository.save(attack); // ์ €์žฅ์†Œ์— ๊ณต๊ฒฉ ์ •๋ณด ์—…๋ฐ์ดํŠธํ•ด ์ €์žฅ
}
return attackDtoList;
}

public AttackDto formatAttackDto(Attack attack){ //time ํ˜•์‹ ์ง€์ •
public AttackDto formatToAttackDto(Attack attack){ // ์‘๋‹ต ํ˜•์‹์„ ๋ณ€ํ™˜(AttackDto; time ํ˜•์‹๋„ ์ง€์ •ํ•œ ํŒจํ„ด์œผ๋กœ ๋ณ€ํ™˜)
LocalDateTime attackedTime = attack.getTime();
String timeString = attackedTime.format(DateTimeFormatter.ofPattern("yyyy.MM.dd HH:mm"));
Long memberId = attack.getAttackerId();
String attackerName = validationService.valMember(memberId).getNickname();
return AttackDto.builder()
.time(timeString)
.attacker(attackerName)
.attacker(attackerName) // ๊ณต๊ฒฉ์ž์˜ ์ด๋ฆ„์„ ์ฐพ์•„ Stringํ˜•์œผ๋กœ ์ €์žฅ
.isChecked(attack.getIsChecked())
.build();
}

// AttackController์˜ attackUser ๊ด€๋ จ

public Boolean tryAttack(String attackedName){
public Boolean tryAttack(String attackedName){ // ๊ณต๊ฒฉ์„ ์‹œ๋„ํ•จ
Member member = memberService.getAuthMember();
if (!member.useSnowflake()) { // ๋ˆˆ์†ก์ด ์†Œ๋ชจ ๋ถˆ๊ฐ€
if (!member.useSnowflake()) { // true: ๋ˆˆ์†ก์ด ์†Œ๋ชจํ•จ, false: ๋ˆˆ์†ก์ด ์†Œ๋ชจ ๋ถˆ๊ฐ€
return false;
}
memberRepository.save(member);
createAttack(member.getId(), attackedName);
memberRepository.save(member); // ๋ณ€๊ฒฝ๋œ ๋ˆˆ์†ก์ด ๊ฐฏ์ˆ˜ ์ €์žฅ
createAttack(member.getId(), attackedName); // ๊ณต๊ฒฉ ์ƒ์„ฑ
return true;
}

public void createAttack(Long attackerId, String attackedName){
LocalDateTime now = LocalDateTime.now(); // ํ˜„์žฌ ์‹œ๊ฐ
Member attackedMember = validationService.valMember(attackedName);
attackedMember.alarmUnchecked(); // ๊ณต๊ฒฉ๋ฐ›์€ ์‚ฌ๋žŒ์˜ newAlarm ์„ true ๋กœ ๋งŒ๋“ฌ
LocalDateTime time = LocalDateTime.now(); // ํ˜„์žฌ ์‹œ๊ฐ
Member attackedMember = validationService.valMember(attackedName); // ๊ณต๊ฒฉ๋ฐ›์€ ์‚ฌ๋žŒ
attackedMember.alarmUnchecked(); // ๊ณต๊ฒฉ๋ฐ›์€ ์‚ฌ๋žŒ์˜ newAlarm ์„ true ๋กœ
Attack newAttack = Attack.builder()
.time(now)
.time(time)
.attackerId(attackerId)
.isChecked(false)
.member(attackedMember)
.build();
Long snowmanHeight = attackedMember.getSnowmanHeight();
if (snowmanHeight > 1) {
attackedMember.updateSnowmanHeight(snowmanHeight - 1); // ๊ณต๊ฒฉ๋ฐ›์€ ์‚ฌ๋žŒ ๋ˆˆ์‚ฌ๋žŒ ํ‚ค ์ค„์ด๊ธฐ
}
memberRepository.save(attackedMember);
attackedMember = memberService.refreshHeight(attackedMember, -1L, attackedMember.getSnowmanHeight() - 1); // ๊ณต๊ฒฉ๋ฐ›์€ ์‚ฌ๋žŒ์˜ ๋ˆˆ์‚ฌ๋žŒ ํ‚ค๋ฅผ ์ค„์ž„
log.info("memberService ๋ฐ˜ํ™˜๊ฐ’ -> {}", attackedMember);
attackRepository.save(newAttack);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.committers.snowflowerthon.committersserver.domain.follow.controller;

import com.committers.snowflowerthon.committersserver.common.response.ApiResponse;
import com.committers.snowflowerthon.committersserver.common.response.exception.ErrorCode;
import com.committers.snowflowerthon.committersserver.domain.follow.dto.FollowPatchedDto;
import com.committers.snowflowerthon.committersserver.domain.follow.service.FollowService;
import lombok.RequiredArgsConstructor;
Expand All @@ -8,14 +10,17 @@

@RestController
@RequiredArgsConstructor
@CrossOrigin(origins = "https://kidari.site")
@CrossOrigin(origins = "https://kidari.site", allowedHeaders = "*")
@RequestMapping("/api/v1")
public class FollowController {

private final FollowService followService;

@PatchMapping("/buddy/update")
public ResponseEntity<FollowPatchedDto> patchFollow(@RequestParam String nickname, @RequestParam boolean isFollowed) {
public ResponseEntity<?> patchFollow(@RequestParam String nickname, @RequestParam Boolean isFollowed) {
FollowPatchedDto followPatchedDto = followService.changeFollowStatus(nickname, isFollowed);
return ResponseEntity.ok(followPatchedDto);
if (followPatchedDto == null)
return ResponseEntity.badRequest().body(ApiResponse.failure(ErrorCode.FOLLOW_BAD_REQUEST));
return ResponseEntity.ok().body(ApiResponse.success(followPatchedDto));
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package com.committers.snowflowerthon.committersserver.domain.follow.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Builder
@Getter
@AllArgsConstructor
@NoArgsConstructor
public class FollowPatchedDto {
private String nickname;
private boolean isFollowed;
Expand Down
Loading

0 comments on commit 947df18

Please sign in to comment.