Skip to content

Commit

Permalink
Merge pull request #17 from GDSC-snowflowerthon/feature/10
Browse files Browse the repository at this point in the history
[Feature] login의 reponse와 getSubgoalInfoByUserId의 response 수정
  • Loading branch information
soHyn authored Jan 12, 2024
2 parents 2dd8265 + 45d8b73 commit db991ba
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import tenten.blooming.domain.subgoal.entity.Subgoal;
import tenten.blooming.domain.subgoal.repository.SubgoalRepository;
import tenten.blooming.domain.subgoal.service.SubgoalService;
import tenten.blooming.domain.user.entity.User;
import tenten.blooming.domain.user.repository.UserRepository;

import java.time.LocalDate;
import java.util.List;
Expand All @@ -21,6 +23,7 @@ public class SubgoalController {

private final SubgoalService subgoalService;
private final SubgoalRepository subgoalRepository;
private final UserRepository userRepository;

@PostMapping("/subgoal/{goalId}/detail/{subgoalId}")
public ResponseEntity<ResponseUpdateSubgoal> updateSubgoal(
Expand All @@ -45,6 +48,9 @@ public ResponseEntity<SubgoalResponse> getSubgoalInfoByUserId(
) {
SubgoalResponse subgoalResponse = subgoalService.getSubgoalInfoByUserId(request.getUserId());

User findUser = userRepository.findById(request.getUserId()).orElse(null);
subgoalResponse.setNickname(findUser.getNickname());

return ResponseEntity.ok(subgoalResponse);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class SubgoalResponse {
private Long goalId;
private LocalDate goalCreateDate;
private List<SubgoalInfo> SubgoalList;
private String nickname;

@Data
@NoArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public SubgoalResponse getGoalInfo(Goal goal) {
List<Subgoal> subgoals = goal.getSubgoals();
List<SubgoalResponse.SubgoalInfo> subgoalInfoList = new ArrayList<>();


SubgoalResponse subgoalResponse = new SubgoalResponse();

for(Subgoal subgoal : subgoals) {
Expand Down Expand Up @@ -118,16 +117,17 @@ public CompletedGoalInfoResponse getCompletedGoalInfo(Long userId) {
public List<LocalDate> addDoneDate(Long subgoalId) {
Subgoal findSubgoal = subgoalRepository.findById(subgoalId).orElse(null);
List<LocalDate> doneDates = findSubgoal.getDoneDates();
final LocalDate today = LocalDate.now();

for(int i = 0; i < doneDates.size(); i++) {
if(doneDates.get(i) == null) {
if(i != 0 && (doneDates.get(i-1) == LocalDate.now())) {
if(i != 0 && (doneDates.get(i-1) == today)) {
throw new IllegalStateException("이미 체크된 TASK입니다.");
}
System.out.println(i);
switch (i + 1) {
case 1: {
findSubgoal.setDoneDate1(LocalDate.now());
findSubgoal.setDoneDate1(today);
subgoalRepository.save(findSubgoal);
break;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package tenten.blooming.domain.user.controller;

import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import tenten.blooming.domain.user.dto.LoginResponse;
import tenten.blooming.domain.user.entity.User;
import tenten.blooming.domain.user.dto.UserForm;
import tenten.blooming.domain.user.service.UserService;
Expand Down Expand Up @@ -35,25 +37,36 @@ public ResponseEntity<BasicResponse> signup(@RequestBody UserForm dto) {
}

@PostMapping("/login")
public ResponseEntity<BasicResponse> login(@RequestBody UserForm dto) {
public ResponseEntity<LoginResponse> login(@RequestBody UserForm dto) {
try {
User user = userService.login(dto.getLoginId(), dto.getPassword());

BasicResponse basicResponse = BasicResponse.builder()
.code(HttpStatus.OK.value())
.message("로그인에 성공하였습니다.")
.result(user)
.build();
LoginResponse.LoginResult loginResult = new LoginResponse.LoginResult();

return new ResponseEntity<>(basicResponse, HttpStatus.OK);
Long activeGoalId = userService.getActiveGoalIdByLoginId(dto.getLoginId());

loginResult.setLoginId(user.getLoginId());
loginResult.setPassword(user.getPassword());
loginResult.setNickname(user.getNickname());
loginResult.setHasGoal(user.getHasGoal());
loginResult.setCreatedAt(user.getCreatedAt());
loginResult.setUserId(user.getUserId());
loginResult.setActiveGoalId(activeGoalId);

LoginResponse loginResponse = LoginResponse.builder()
.code(HttpStatus.OK.value())
.message("로그인에 성공했습니다.")
.result(loginResult)
.build();
return new ResponseEntity<>(loginResponse, HttpStatus.OK);
} catch (DataIntegrityViolationException e) {
BasicResponse basicResponse = BasicResponse.builder()
LoginResponse loginResponse = LoginResponse.builder()
.code(HttpStatus.BAD_REQUEST.value())
.message("로그인에 실패했습니다.")
.result(null)
.build();

return new ResponseEntity<>(basicResponse, HttpStatus.BAD_REQUEST);
return new ResponseEntity<>(loginResponse, HttpStatus.BAD_REQUEST);
}
}
}
33 changes: 33 additions & 0 deletions src/main/java/tenten/blooming/domain/user/dto/LoginResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package tenten.blooming.domain.user.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.sql.Timestamp;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class LoginResponse {
private Integer code;
private String message;
private LoginResult result;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public static class LoginResult {
private Long userId;
private String loginId;
private String nickname;
private String password;
private Boolean hasGoal = false;
private Timestamp createdAt;
private Long activeGoalId;
}

}
13 changes: 13 additions & 0 deletions src/main/java/tenten/blooming/domain/user/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
import tenten.blooming.domain.goal.entity.Goal;
import tenten.blooming.domain.user.repository.UserRepository;
import tenten.blooming.domain.user.dto.UserForm;
import tenten.blooming.domain.user.entity.User;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserService {
@Autowired
Expand Down Expand Up @@ -36,4 +40,13 @@ public User login(String loginId, String password) {
throw new DataIntegrityViolationException("로그인에 실패하였습니다.");
}
}

public Long getActiveGoalIdByLoginId(String loginId) {
User findUser = userRepository.findByLoginId(loginId);

List<Goal> goals = findUser.getGoals();
Goal activeGoal = goals.get(goals.size() - 1);

return activeGoal.getGoalId();
}
}

0 comments on commit db991ba

Please sign in to comment.