Skip to content

Commit

Permalink
Merge pull request #59 from dnd-side-project/dev
Browse files Browse the repository at this point in the history
release v0.0.1
  • Loading branch information
eun-seong authored Feb 21, 2024
2 parents 212932f + 0eeb676 commit 3b17ac0
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.dnd.namuiwiki.domain.survey;

import com.dnd.namuiwiki.domain.survey.model.entity.Survey;
import com.dnd.namuiwiki.domain.user.entity.User;
import org.springframework.data.mongodb.repository.MongoRepository;

public interface SurveyRepository extends MongoRepository<Survey, String> {
Long countByOwner(User owner);
}
47 changes: 47 additions & 0 deletions src/main/java/com/dnd/namuiwiki/domain/user/UserController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.dnd.namuiwiki.domain.user;

import com.dnd.namuiwiki.common.dto.ResponseDto;
import com.dnd.namuiwiki.domain.jwt.JwtAuthorization;
import com.dnd.namuiwiki.domain.jwt.dto.TokenUserInfoDto;
import com.dnd.namuiwiki.domain.user.dto.EditUserProfileRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1/users")
@RequiredArgsConstructor
public class UserController {
private final UserService userService;

@GetMapping()
public ResponseEntity<?> getUserPublicProfile(
@RequestParam String wikiId
) {
var response = userService.getUserPublicProfile(wikiId);
return ResponseDto.ok(response);
}

@GetMapping("/profile")
public ResponseEntity<?> getMyProfile(
@JwtAuthorization TokenUserInfoDto tokenUserInfoDto
) {
var response = userService.getMyProfile(tokenUserInfoDto);
return ResponseDto.ok(response);
}

@PutMapping("/profile")
public ResponseEntity<?> editMyProfile(
@JwtAuthorization TokenUserInfoDto tokenUserInfoDto,
@RequestBody EditUserProfileRequest request
) {
userService.editMyProfile(tokenUserInfoDto, request);
return ResponseDto.noContent();
}

}
34 changes: 34 additions & 0 deletions src/main/java/com/dnd/namuiwiki/domain/user/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@
import com.dnd.namuiwiki.domain.auth.dto.SignUpResponse;
import com.dnd.namuiwiki.domain.jwt.JwtService;
import com.dnd.namuiwiki.domain.jwt.dto.TokenPairDto;
import com.dnd.namuiwiki.domain.jwt.dto.TokenUserInfoDto;
import com.dnd.namuiwiki.domain.oauth.OAuthService;
import com.dnd.namuiwiki.domain.oauth.dto.OAuthUserInfoDto;
import com.dnd.namuiwiki.domain.oauth.type.OAuthProvider;
import com.dnd.namuiwiki.domain.survey.SurveyRepository;
import com.dnd.namuiwiki.domain.user.dto.EditUserProfileRequest;
import com.dnd.namuiwiki.domain.user.dto.GetUserPublicProfileResponse;
import com.dnd.namuiwiki.domain.user.dto.UserProfileDto;
import com.dnd.namuiwiki.domain.user.entity.User;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
Expand All @@ -26,6 +31,7 @@ public class UserService {
private final String COOKIE_OAUTH_PROVIDER = "oauthProvider";

private final UserRepository userRepository;
private final SurveyRepository surveyRepository;
private final JwtService jwtService;
private final OAuthService oAuthService;

Expand Down Expand Up @@ -63,6 +69,12 @@ public SignUpResponse signUp(String oauthProvider, String oauthAccessToken, Stri
return SignUpResponse.from(tokenPair);
}

public GetUserPublicProfileResponse getUserPublicProfile(String wikiId) {
User user = userRepository.findByWikiId(wikiId)
.orElseThrow(() -> new ApplicationErrorException(ApplicationErrorType.NOT_FOUND_USER));
return new GetUserPublicProfileResponse(user.getNickname());
}

private MultiValueMap<String, String> getOauthCookie(OAuthUserInfoDto oAuthUserInfoDto) {
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
ResponseCookie oauthToken = ResponseCookie.from(COOKIE_OAUTH_ACCESSTOKEN, oAuthUserInfoDto.getAccessToken())
Expand All @@ -83,4 +95,26 @@ private MultiValueMap<String, String> getOauthCookie(OAuthUserInfoDto oAuthUserI
return headers;
}


public UserProfileDto getMyProfile(TokenUserInfoDto tokenUserInfoDto) {
String wikiId = tokenUserInfoDto.getWikiId();
User user = getUserByWikiId(wikiId);

Long totalSurveyCnt = surveyRepository.countByOwner(user);

return new UserProfileDto(user.getWikiId(), user.getNickname(), totalSurveyCnt);
}

public void editMyProfile(TokenUserInfoDto tokenUserInfoDto, EditUserProfileRequest request) {
User user = getUserByWikiId(tokenUserInfoDto.getWikiId());
user.setNickname(request.getNickname());

userRepository.save(user);
}

private User getUserByWikiId(String wikiId) {
return userRepository.findByWikiId(wikiId)
.orElseThrow(() -> new ApplicationErrorException(ApplicationErrorType.NOT_FOUND_USER));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.dnd.namuiwiki.domain.user.dto;

import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.Size;
import lombok.Getter;

@Getter
public class EditUserProfileRequest {

@NotEmpty
@Size(min = 2, max = 6, message = "๋‹‰๋„ค์ž„์˜ ๊ธธ์ด๋Š” 2 ์ด์ƒ 6 ์ดํ•˜์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.")
private String nickname;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.dnd.namuiwiki.domain.user.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;

@AllArgsConstructor
@Getter
public class GetUserPublicProfileResponse {
private String nickname;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.dnd.namuiwiki.domain.user.dto;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public class UserProfileDto {
private final String wikiId;
private final String name;
private final Long totalSurveyCnt;
}
4 changes: 4 additions & 0 deletions src/main/java/com/dnd/namuiwiki/domain/user/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public void setRefreshToken(String refreshToken) {
this.refreshToken = refreshToken;
}

public void setNickname(String nickname) {
this.nickname = nickname;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down

0 comments on commit 3b17ac0

Please sign in to comment.