-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BE] 이근표 로그인 #4
Open
rootTiket
wants to merge
14
commits into
Leets-Official:main
Choose a base branch
from
rootTiket:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[BE] 이근표 로그인 #4
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
90a64d8
[feat]: user 엔티티 생성, 초기설정
rootTiket aa54c0f
[feat]: attendance 엔티티 생성
rootTiket ed65769
[feat]: 회원가입 기능 구현
rootTiket 179ffcd
[refactor]: 중복 변수 삭제
rootTiket 2d34814
[fix]: Attendance fk 누락 수정
rootTiket aff95e4
[feat]: 초기 Attendance 생성 로직 구현
rootTiket 493c078
[feat]: 로그인 로직구현
rootTiket 7729b7d
[feat]: id 중복확인 api 구현
rootTiket dda39ca
[feat]: 토큰 구현
rootTiket 0a091bb
[feat]: 출석체크 기능 구현
rootTiket 8fbd10d
[feat]: 출석기록, 출석률 조회하는 기능구현
rootTiket 2cb689e
[fix]: 출석률의 소수점을 없애도록 변경
rootTiket 3193990
[refactor]: 반복문 하드코딩이 되어있는 부분 수정
rootTiket 8b282f6
[refactor]: 메서드 분할
rootTiket File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/main/java/leets/attendance/domain/attendance/Repository/AttendanceRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package leets.attendance.domain.attendance.Repository; | ||
|
||
import leets.attendance.domain.attendance.domain.Attendance; | ||
import leets.attendance.domain.user.domain.User; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public interface AttendanceRepository extends JpaRepository<Attendance,Long> { | ||
Optional<Attendance> findByAttendanceId(Long attendanceId); | ||
Optional<Attendance> findByWeekAndUser(Integer week, User user); | ||
List<Attendance> findByUser(User user); | ||
List<Attendance> findByUserAndIsAttend(User user, Boolean isAttend); | ||
} |
102 changes: 102 additions & 0 deletions
102
src/main/java/leets/attendance/domain/attendance/application/AttendanceService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package leets.attendance.domain.attendance.application; | ||
|
||
|
||
import leets.attendance.domain.attendance.Repository.AttendanceRepository; | ||
import leets.attendance.domain.attendance.domain.Attendance; | ||
import leets.attendance.domain.attendance.dto.AttendanceInfo; | ||
import leets.attendance.domain.attendance.dto.AttendanceResponseDto; | ||
import leets.attendance.domain.attendance.exception.InvalidDayException; | ||
import leets.attendance.domain.attendance.presentation.AttendanceMessageEnum; | ||
import leets.attendance.domain.user.domain.User; | ||
import leets.attendance.domain.user.repository.UserRepository; | ||
import leets.attendance.global.DateEnum; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.time.DayOfWeek; | ||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class AttendanceService { | ||
private final AttendanceRepository attendanceRepository; | ||
private final UserRepository userRepository; | ||
|
||
public void createInitialAttendance(User user) { | ||
for (int i = 1; i <= DateEnum.values().length; i++) { | ||
Attendance attendance = Attendance.builder() | ||
.isAttend(false) | ||
.week(i) | ||
.attendanceTime(DateEnum.getByWeekNumber(i).getStartDate()) | ||
.user(user) | ||
.build(); | ||
attendanceRepository.save(attendance); | ||
} | ||
} | ||
|
||
public String updateAttendance(Authentication authentication) { | ||
String id = authentication.getName(); | ||
User user = userRepository.findById(id).get(); | ||
LocalDateTime date = LocalDateTime.now(); | ||
DateEnum dateEnum = isValidWeek(date); | ||
Attendance attendance = attendanceRepository.findByWeekAndUser(parseWeek(dateEnum),user).get(); | ||
Attendance newAttendance = Attendance.builder() | ||
.attendanceId(attendance.getAttendanceId()) | ||
.isAttend(true) | ||
.user(user) | ||
.week(attendance.getWeek()) | ||
.build(); | ||
attendanceRepository.save(newAttendance); | ||
return AttendanceMessageEnum.UPDATE_SUCCESS.getMessage(); | ||
} | ||
|
||
public DateEnum isValidWeek(LocalDateTime today) { | ||
try { | ||
if (today.getHour() != 19 || !(today.getDayOfWeek() == DayOfWeek.THURSDAY)) { | ||
throw new InvalidDayException(); | ||
} | ||
LocalDate validDate = today.with(DayOfWeek.THURSDAY).toLocalDate(); | ||
return DateEnum.getByDate(validDate); | ||
} catch (IllegalArgumentException e) { | ||
throw new InvalidDayException(); | ||
} | ||
} | ||
|
||
public Integer parseWeek(DateEnum dateEnum) { | ||
String weekNumberStr = dateEnum.name().replace("WEEK_", ""); | ||
return Integer.parseInt(weekNumberStr); | ||
} | ||
|
||
public AttendanceResponseDto getAttendances(Authentication authentication) { | ||
User user = userRepository.findById(authentication.getName()).get(); | ||
List<Attendance> attendances = attendanceRepository.findByUser(user); | ||
List<AttendanceInfo> attendanceInfos = attendances.stream() | ||
.map(a -> new AttendanceInfo(a.getWeek(), a.getIsAttend())) | ||
.collect(Collectors.toList()); | ||
|
||
return AttendanceResponseDto.builder() | ||
.attendanceList(attendanceInfos) | ||
.name(user.getName()) | ||
.date(LocalDate.now()) | ||
.build(); | ||
} | ||
|
||
public int dateToInt(LocalDateTime date) { | ||
LocalDate localDate = date.with(DayOfWeek.THURSDAY).toLocalDate(); | ||
String weekNumberStr = DateEnum.getByDate(localDate).name().replace("WEEK_", ""); | ||
return Integer.parseInt(weekNumberStr); | ||
} | ||
|
||
public String getAttendanceRates(Authentication authentication) { | ||
User user = userRepository.findById(authentication.getName()).get(); | ||
LocalDateTime date = LocalDateTime.now(); | ||
double trueValue = attendanceRepository.findByUserAndIsAttend(user,true).size(); | ||
double dateSize = attendanceRepository.findByUser(user).stream().filter(a -> a.getWeek() <= dateToInt(date)).toList().size(); | ||
double result = (trueValue / dateSize) * 100; | ||
return (int) result + "%"; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/leets/attendance/domain/attendance/domain/Attendance.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package leets.attendance.domain.attendance.domain; | ||
|
||
|
||
import jakarta.persistence.*; | ||
import leets.attendance.domain.user.domain.User; | ||
import leets.attendance.global.BaseTimeEntity; | ||
import lombok.*; | ||
|
||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
|
||
|
||
@Setter | ||
@Getter | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Entity | ||
@Table | ||
public class Attendance extends BaseTimeEntity { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column | ||
private Long attendanceId; | ||
|
||
@Column(nullable = false) | ||
private Integer week; | ||
|
||
@Column(nullable = false) | ||
private Boolean isAttend; | ||
|
||
@Column | ||
private LocalDate attendanceTime; | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "user_id") | ||
private User user; | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/leets/attendance/domain/attendance/dto/AttendanceInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package leets.attendance.domain.attendance.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
@Builder | ||
@NoArgsConstructor | ||
public class AttendanceInfo { | ||
private int week; | ||
private boolean isAttend; | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/leets/attendance/domain/attendance/dto/AttendanceResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package leets.attendance.domain.attendance.dto; | ||
|
||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.time.LocalDate; | ||
import java.util.List; | ||
|
||
@Builder | ||
@Getter | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class AttendanceResponseDto { | ||
private String name; | ||
private LocalDate date; | ||
private List<AttendanceInfo> attendanceList; | ||
|
||
} | ||
|
10 changes: 10 additions & 0 deletions
10
src/main/java/leets/attendance/domain/attendance/exception/InvalidDayException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package leets.attendance.domain.attendance.exception; | ||
|
||
import leets.attendance.global.error.ErrorCode; | ||
import leets.attendance.global.error.ServiceException; | ||
|
||
public class InvalidDayException extends ServiceException { | ||
public InvalidDayException(){ | ||
super(ErrorCode.INVALID_TIME.getHttpStatus(),ErrorCode.INVALID_TIME.getMessage()); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/main/java/leets/attendance/domain/attendance/presentation/AttendanceController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package leets.attendance.domain.attendance.presentation; | ||
|
||
|
||
import leets.attendance.domain.attendance.application.AttendanceService; | ||
import leets.attendance.domain.attendance.dto.AttendanceResponseDto; | ||
import leets.attendance.global.dto.ResponseDto; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PatchMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import static leets.attendance.domain.attendance.presentation.AttendanceMessageEnum.GET_SUCCESS; | ||
import static org.springframework.http.HttpStatus.OK; | ||
|
||
@RestController | ||
@RequestMapping("/attendances") | ||
public class AttendanceController { | ||
|
||
private final AttendanceService attendanceService; | ||
|
||
public AttendanceController(AttendanceService attendanceService) { | ||
this.attendanceService = attendanceService; | ||
} | ||
|
||
@PatchMapping | ||
public ResponseDto<String> updateAttendance(Authentication authentication) { | ||
return ResponseDto.of(OK.value(), attendanceService.updateAttendance(authentication)); | ||
} | ||
|
||
@GetMapping | ||
public ResponseDto<AttendanceResponseDto> getAttendances(Authentication authentication) { | ||
return ResponseDto.of(OK.value(),GET_SUCCESS.getMessage(),attendanceService.getAttendances(authentication)); | ||
} | ||
|
||
@GetMapping(value = "/rates") | ||
public ResponseDto<String> getAttendanceRates(Authentication authentication) { | ||
return ResponseDto.of(OK.value(),GET_SUCCESS.getMessage(),attendanceService.getAttendanceRates(authentication)); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/leets/attendance/domain/attendance/presentation/AttendanceMessageEnum.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package leets.attendance.domain.attendance.presentation; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@AllArgsConstructor | ||
@Getter | ||
public enum AttendanceMessageEnum { | ||
UPDATE_SUCCESS("출석 갱신에 성공하였습니다."), | ||
GET_SUCCESS("목록조회에 성공하였습니다"); | ||
private String message; | ||
} |
68 changes: 68 additions & 0 deletions
68
src/main/java/leets/attendance/domain/user/application/UserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package leets.attendance.domain.user.application; | ||
|
||
|
||
import leets.attendance.domain.attendance.application.AttendanceService; | ||
import leets.attendance.domain.user.domain.User; | ||
import leets.attendance.domain.user.dto.LoginRequest; | ||
import leets.attendance.domain.user.dto.UserRequest; | ||
import leets.attendance.domain.user.dto.UserResponse; | ||
import leets.attendance.domain.user.exception.InvalidIdException; | ||
import leets.attendance.domain.user.exception.InvalidPasswordException; | ||
import leets.attendance.domain.user.exception.ConflictIdException; | ||
import leets.attendance.domain.user.repository.UserRepository; | ||
import leets.attendance.global.jwt.TokenProvider; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class UserService { | ||
private final UserRepository userRepository; | ||
private final AttendanceService attendanceService; | ||
private final TokenProvider tokenProvider; | ||
public UserResponse register(UserRequest userRequest) throws Exception{ | ||
String id = userRequest.getId(); | ||
if(userRepository.findById(id).isPresent()){ | ||
throw new ConflictIdException(); | ||
} | ||
User user = User.builder() | ||
.id(id) | ||
.name(userRequest.getName()) | ||
.part(userRequest.getPart()) | ||
.password(userRequest.getPassword()) | ||
.build(); | ||
userRepository.save(user); | ||
attendanceService.createInitialAttendance(user); | ||
return UserResponse.builder() | ||
.userId(user.getUserId()) | ||
.name(user.getName()) | ||
.part(user.getPart()) | ||
.build(); | ||
} | ||
|
||
public UserResponse login(LoginRequest loginRequest) throws Exception{ | ||
String id = loginRequest.getId(); | ||
if(!userRepository.findById(id).isPresent()){ | ||
throw new InvalidIdException(); | ||
} | ||
String password = loginRequest.getPassword(); | ||
User user = userRepository.findById(id).orElseThrow(Exception::new); | ||
if(!user.getPassword().equals(password)){ | ||
throw new InvalidPasswordException(); | ||
} | ||
String token = tokenProvider.createToken(user.getId()); | ||
return UserResponse.builder() | ||
.userId(user.getUserId()) | ||
.name(user.getName()) | ||
.part(user.getPart()) | ||
.token(token) | ||
.build(); | ||
} | ||
|
||
public String checkDuplicateId(String id) throws Exception{ | ||
if(userRepository.findById(id).isPresent()){ | ||
throw new ConflictIdException(); | ||
} | ||
return id; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/leets/attendance/domain/user/domain/User.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package leets.attendance.domain.user.domain; | ||
|
||
import jakarta.persistence.*; | ||
import lombok.*; | ||
import java.util.UUID; | ||
|
||
@Setter | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
@Getter | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Entity | ||
@Table | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
public class User { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.UUID) | ||
@Column | ||
private UUID userId; | ||
|
||
@Column(nullable = false) | ||
private String id; | ||
|
||
@Column(nullable = false) | ||
private String password; | ||
|
||
@Column(nullable = false) | ||
private String name; | ||
|
||
@Column(nullable = false) | ||
private String part; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.orElseThrow(UserNotFoundExcepiton::new);
으로 고치시면 좀 더 확실한 에러처리가 될것 같아요!exception에 잘 구현하신거 같은데
UserNotFoundExcepiton
추가하시면 됩니다~