Skip to content

Commit

Permalink
Merge pull request #14 from SOPT-33-iOS-Team-1/feat/#11-get-main-view
Browse files Browse the repository at this point in the history
Feat/#11 get main view
  • Loading branch information
yummygyudon authored Nov 25, 2023
2 parents ed70233 + 25f49cb commit 27ef4e3
Show file tree
Hide file tree
Showing 10 changed files with 216 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.sopt.sopkerton.common.exception.base.ErrorBase;
import org.sopt.sopkerton.common.exception.base.SuccessBase;

@Getter
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class ApiResponse<T> {
Expand All @@ -21,7 +23,7 @@ public static ApiResponse<?> success(SuccessBase success) {
}

public static <T> ApiResponse<T> success(SuccessBase success, T data) {
return new ApiResponse<T>(success.getHttpStatusCode(), success.getSuccessMessage(), data);
return new ApiResponse<>(success.getHttpStatusCode(), success.getSuccessMessage(), data);
}

public static <T> ApiResponse <T> error(ErrorBase error) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.sopt.sopkerton.user.controller;

import lombok.RequiredArgsConstructor;
import org.sopt.sopkerton.common.response.ApiResponse;
import org.sopt.sopkerton.user.domain.exception.UserSuccess;
import org.sopt.sopkerton.user.dto.response.MainView;
import org.sopt.sopkerton.user.service.UserService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/user")
public class UserController {

private final UserService userService;

@GetMapping("/info/{userId}")
public ResponseEntity<ApiResponse<MainView>> orderMainView(
@PathVariable("userId") Long userId
) {
MainView mainViewInfo = userService.getMainViewInfo(userId);
return ResponseEntity
.status(UserSuccess.USER_MAIN_VIEW_SUCCESS.getHttpStatus())
.body(
ApiResponse.success(UserSuccess.USER_MAIN_VIEW_SUCCESS, mainViewInfo)
);
}
}
33 changes: 33 additions & 0 deletions src/main/java/org/sopt/sopkerton/user/domain/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.sopt.sopkerton.user.domain;

import jakarta.persistence.*;
import lombok.*;
import org.sopt.sopkerton.user.domain.enums.Gender;

@Getter
@Entity
@Table(schema = "skt-t1-app", name = "users")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Builder
public class User {

@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;

@Column(name = "name", nullable = false)
String name;

@Column(name = "gender", nullable = false)
@Enumerated(value = EnumType.STRING)
Gender gender;

@Column(name = "thumbnail")
String thumbnail;

@Column(name = "age", nullable = false)
int age;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.sopt.sopkerton.user.domain.enums;

public enum Gender {
MALE, FEMALE
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.sopt.sopkerton.user.domain.exception;

import lombok.AllArgsConstructor;
import org.sopt.sopkerton.common.exception.base.ErrorBase;
import org.springframework.http.HttpStatus;

@AllArgsConstructor
public enum UserError implements ErrorBase {
USER_NOT_FOUND(HttpStatus.NOT_FOUND, "Can not found User."),
;

private final HttpStatus status;
private final String errorMessage;


@Override
public int getHttpStatusCode() {
return this.status.value();
}

@Override
public HttpStatus getHttpStatus() {
return this.status;
}

@Override
public String getErrorMessage() {
return this.errorMessage;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.sopt.sopkerton.user.domain.exception;

import org.sopt.sopkerton.common.exception.base.ExceptionBase;

public class UserException extends ExceptionBase {
public UserException(UserError userError) {
super(userError);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.sopt.sopkerton.user.domain.exception;

import lombok.AllArgsConstructor;
import org.sopt.sopkerton.common.exception.base.SuccessBase;
import org.springframework.http.HttpStatus;

@AllArgsConstructor
public enum UserSuccess implements SuccessBase {
USER_MAIN_VIEW_SUCCESS(HttpStatus.OK, "Get User Main View Data Successful.")
;

private final HttpStatus status;
private final String successMessage;

@Override
public int getHttpStatusCode() {
return this.status.value();
}

@Override
public HttpStatus getHttpStatus() {
return this.status;
}

@Override
public String getSuccessMessage() {
return this.successMessage;
}
}
17 changes: 17 additions & 0 deletions src/main/java/org/sopt/sopkerton/user/dto/response/MainView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.sopt.sopkerton.user.dto.response;


public record MainView(
long userId,
String thumbnail,
String name,
String gender, // 남성 || 여성
int age,
String criminalHistory,
String criminalHistoryDate,
int volunteerHours,
int completedProgramCount,
int certificateCount,
double ringRate
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.sopt.sopkerton.user.infrastructure;


import org.sopt.sopkerton.user.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository<User, Long> {
}
50 changes: 50 additions & 0 deletions src/main/java/org/sopt/sopkerton/user/service/UserService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.sopt.sopkerton.user.service;

import lombok.RequiredArgsConstructor;
import org.sopt.sopkerton.user.domain.User;
import org.sopt.sopkerton.user.domain.enums.Gender;
import org.sopt.sopkerton.user.domain.exception.UserError;
import org.sopt.sopkerton.user.domain.exception.UserException;
import org.sopt.sopkerton.user.dto.response.MainView;
import org.sopt.sopkerton.user.infrastructure.UserRepository;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class UserService {
private static final String CRIMINAL_HISTORY = "단순 절도죄";
private static final String CRIMINAL_HISTORY_DATE = "3년전";
private static final String MALE = "남성";
private static final String FEMALE = "여성";

private static final int VOLUNTEER_HOURS = 50;
private static final int COMPLETED_PROGRAM_COUNT = 4;
private static final int CERTIFICATE_COUNT = 4;
private static final double RING_RATE = 0.65;

private final UserRepository userRepository;

public MainView getMainViewInfo(Long userId) {
User targetUser = userRepository.findById(userId)
.orElseThrow(() -> new UserException(UserError.USER_NOT_FOUND));
String gender;
if (targetUser.getGender().equals(Gender.MALE)) {
gender = MALE;
} else {
gender = FEMALE;
}
return new MainView(
targetUser.getId(),
targetUser.getThumbnail(),
targetUser.getName(),
gender,
targetUser.getAge(),
CRIMINAL_HISTORY,
CRIMINAL_HISTORY_DATE,
VOLUNTEER_HOURS,
COMPLETED_PROGRAM_COUNT,
CERTIFICATE_COUNT,
RING_RATE
);
}
}

0 comments on commit 27ef4e3

Please sign in to comment.