Skip to content
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

feat: 9주차 미션 제출 #20

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions 민디_김민기/week9/admin/controller/AdminController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package com.example.icebutler_server.admin.controller;

import com.example.icebutler_server.admin.dto.condition.SearchCond;
import com.example.icebutler_server.admin.dto.request.JoinRequest;
import com.example.icebutler_server.admin.dto.request.LoginRequest;
import com.example.icebutler_server.admin.dto.request.ModifyFoodRequest;
import com.example.icebutler_server.admin.dto.request.RemoveFoodsRequest;
import com.example.icebutler_server.admin.dto.request.WithDrawRequest;
import com.example.icebutler_server.admin.dto.response.*;
import com.example.icebutler_server.admin.dto.response.AdminResponse;
import com.example.icebutler_server.admin.dto.response.LoginResponse;
import com.example.icebutler_server.admin.dto.response.LogoutResponse;
import com.example.icebutler_server.admin.dto.response.SearchFoodsResponse;
import com.example.icebutler_server.admin.dto.response.PostAdminRes;
import com.example.icebutler_server.admin.service.AdminService;
import com.example.icebutler_server.global.dto.response.ResponseCustom;
import com.example.icebutler_server.global.resolver.*;
import com.example.icebutler_server.user.dto.response.MyProfileRes;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RequiredArgsConstructor
@RequestMapping("/admin")
@RestController
public class AdminController {

private final AdminService adminService;

@PostMapping("/join")
public ResponseCustom<AdminResponse> join(@RequestBody JoinRequest request)
{
return ResponseCustom.OK(adminService.join(request));
}

@PostMapping("/login")
public ResponseCustom<PostAdminRes> login(@RequestBody LoginRequest request)
{
return ResponseCustom.OK(adminService.login(request));
}

@Admin
@PostMapping("/logout")
public ResponseCustom<LogoutResponse> logout(@IsAdminLogin AdminLoginStatus loginStatus)
{
adminService.logout(loginStatus.getAdminIdx());
return ResponseCustom.OK();
}

@Admin
@GetMapping("/users")
public ResponseCustom<Page<UserResponse>> search(
@IsAdminLogin AdminLoginStatus loginStatus,
Pageable pageable,
@RequestParam(defaultValue = "") String nickname,
@RequestParam(defaultValue = "true") boolean active
)
{
return ResponseCustom.OK(adminService.search(pageable, nickname, active));
}

@Admin
@DeleteMapping("/users")
public ResponseCustom<Void> withdraw(
@IsAdminLogin AdminLoginStatus loginStatus,
@RequestBody WithDrawRequest request
)
{
adminService.withdraw(request);
return ResponseCustom.OK();
}

// 식품조회
@Admin
@GetMapping("/foods")
public ResponseCustom<Page<SearchFoodsResponse>> searchFoods(@RequestParam String cond, Pageable pageable)
{
return ResponseCustom.OK(adminService.searchFoods(cond, pageable));
}

// 식품수정
@Admin
@PatchMapping("/foods/{foodIdx}")
public ResponseCustom<Void> modifyFood(@PathVariable(name = "foodIdx") Long foodIdx,
@RequestBody ModifyFoodRequest request)
{
adminService.modifyFood(foodIdx, request);
return ResponseCustom.OK();
}

// 식품삭제
@Admin
@DeleteMapping("/foods")
public ResponseCustom<Void> removeFoods(@RequestBody RemoveFoodsRequest request) {
adminService.removeFoods(request);
return ResponseCustom.OK();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.example.icebutler_server.admin.controller;

import com.example.icebutler_server.admin.exception.*;
import com.example.icebutler_server.cart.exception.CartNotFoundException;
import com.example.icebutler_server.global.dto.response.ResponseCustom;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class AdminExceptionController {

@ExceptionHandler(AdminNotFoundException.class)
public ResponseCustom<Void> adminNotFoundException(AdminNotFoundException e){
log.error(e.getMessage());
return ResponseCustom.NOT_FOUND(e.getMessage());
}

@ExceptionHandler(FoodNotFoundException.class)
public ResponseCustom<Void> foodNotFoundException(FoodNotFoundException e){
log.error(e.getMessage());
return ResponseCustom.NOT_FOUND(e.getMessage());
}

@ExceptionHandler(PasswordNotMatchException.class)
public ResponseCustom<Void> passwordNotMatchException(PasswordNotMatchException e){
log.error(e.getMessage());
return ResponseCustom.BAD_REQUEST(e.getMessage());
}


@ExceptionHandler(AlreadyExistEmailException.class)
public ResponseCustom<Void> alreadyExistEmailException(AlreadyExistEmailException e){
log.error(e.getMessage());
return ResponseCustom.BAD_REQUEST(e.getMessage());
}
}
16 changes: 16 additions & 0 deletions 민디_김민기/week9/admin/dto/assembler/AdminAssembler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.example.icebutler_server.admin.dto.assembler;

import com.example.icebutler_server.admin.dto.request.ModifyFoodRequest;
import com.example.icebutler_server.food.entity.Food;
import com.example.icebutler_server.food.entity.FoodCategory;
import org.springframework.stereotype.Component;

@Component
public class AdminAssembler {
public Food toUpdateFoodInfo(Food food, ModifyFoodRequest request) {
if(request.getFoodImgKey() != null) food.toUpdateImgKey(request.getFoodImgKey());
if(request.getFoodName() != null) food.toUpdateName(request.getFoodName());
if(request.getFoodCategory() != null) food.toUpdateCategory(FoodCategory.getFoodCategoryByName(request.getFoodCategory()));
return food;
}
}
8 changes: 8 additions & 0 deletions 민디_김민기/week9/admin/dto/condition/SearchCond.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.example.icebutler_server.admin.dto.condition;

import lombok.Data;

@Data
public class SearchCond {
private String cond;
}
16 changes: 16 additions & 0 deletions 민디_김민기/week9/admin/dto/request/JoinRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.example.icebutler_server.admin.dto.request;

import com.example.icebutler_server.admin.entity.Admin;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
public class JoinRequest {
private String email;
private String password;

public Admin toAdmin(String password) {
return new Admin(this.email, password);
}
}
11 changes: 11 additions & 0 deletions 민디_김민기/week9/admin/dto/request/LoginRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.example.icebutler_server.admin.dto.request;

import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
public class LoginRequest {
private String email;
private String password;
}
15 changes: 15 additions & 0 deletions 민디_김민기/week9/admin/dto/request/ModifyFoodRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.example.icebutler_server.admin.dto.request;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class ModifyFoodRequest {
private String foodCategory;
private String foodName;
private String foodImgKey;
}
10 changes: 10 additions & 0 deletions 민디_김민기/week9/admin/dto/request/RemoveFoodRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.icebutler_server.admin.dto.request;

import lombok.Data;
import lombok.Getter;

@Data
@Getter
public class RemoveFoodRequest {
private Long foodIdx;
}
12 changes: 12 additions & 0 deletions 민디_김민기/week9/admin/dto/request/RemoveFoodsRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example.icebutler_server.admin.dto.request;

import lombok.Data;
import lombok.Getter;

import java.util.List;

@Data
@Getter
public class RemoveFoodsRequest {
private List<RemoveFoodRequest> removeFoods;
}
10 changes: 10 additions & 0 deletions 민디_김민기/week9/admin/dto/request/WithDrawRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.icebutler_server.admin.dto.request;

import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
public class WithDrawRequest {
private Long userIdx;
}
19 changes: 19 additions & 0 deletions 민디_김민기/week9/admin/dto/response/AdminResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.example.icebutler_server.admin.dto.response;

import com.example.icebutler_server.admin.entity.Admin;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
public class AdminResponse {
private String email;

public static AdminResponse toDto(Admin saveAdmin) {
return new AdminResponse(saveAdmin.getEmail());
}

public AdminResponse(String email){
this.email = email;
}
}
9 changes: 9 additions & 0 deletions 민디_김민기/week9/admin/dto/response/LoginResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.icebutler_server.admin.dto.response;

import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
public class LoginResponse {
}
9 changes: 9 additions & 0 deletions 민디_김민기/week9/admin/dto/response/LogoutResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.icebutler_server.admin.dto.response;

import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
public class LogoutResponse {
}
28 changes: 28 additions & 0 deletions 민디_김민기/week9/admin/dto/response/PostAdminRes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.example.icebutler_server.admin.dto.response;

import lombok.Builder;
import lombok.Data;

import static com.example.icebutler_server.global.util.Constant.COMMA;

@Data
public class PostAdminRes {
private String accessToken;
private String refreshToken;

@Builder
public PostAdminRes(String accessToken, String refreshToken) {
this.accessToken = accessToken;
this.refreshToken = refreshToken;
}

public static PostAdminRes toDto(String token) {
String accessToken = token.split(COMMA)[0];
String refreshToken = token.split(COMMA)[1];

return PostAdminRes.builder()
.accessToken(accessToken)
.refreshToken(refreshToken)
.build();
}
}
34 changes: 34 additions & 0 deletions 민디_김민기/week9/admin/dto/response/SearchFoodsResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.example.icebutler_server.admin.dto.response;

import com.example.icebutler_server.food.entity.Food;
import com.example.icebutler_server.food.entity.FoodCategory;
import com.example.icebutler_server.global.util.AwsS3ImageUrlUtil;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
public class SearchFoodsResponse {
private Long foodIdx;
private String foodCategory;
private String foodName;
private String foodImgUrl;

public static SearchFoodsResponse toDto(Food food) {
SearchFoodsResponse searchFoodsResponse = new SearchFoodsResponse();
searchFoodsResponse.foodIdx = food.getFoodIdx();
searchFoodsResponse.foodCategory = food.getFoodCategory().getName();
searchFoodsResponse.foodName = food.getFoodName();
searchFoodsResponse.foodImgUrl = AwsS3ImageUrlUtil.toUrl(food.getFoodImgKey());
return searchFoodsResponse;
}

@Builder
public SearchFoodsResponse(Long foodIdx, String foodCategory, String foodName, String foodImgUrl) {
this.foodIdx = foodIdx;
this.foodCategory = foodCategory;
this.foodName = foodName;
this.foodImgUrl = foodImgUrl;
}
}
27 changes: 27 additions & 0 deletions 민디_김민기/week9/admin/dto/response/UserResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.example.icebutler_server.admin.dto.response;

import com.example.icebutler_server.user.entity.User;
import lombok.Data;
import lombok.NoArgsConstructor;

@NoArgsConstructor
@Data
public class UserResponse {
private Long userIdx;
private String nickname;
private String email;
private String provider;
private boolean isDenied;


public static UserResponse toDto(User user)
{
UserResponse userResponse = new UserResponse();
userResponse.userIdx = user.getUserIdx();
userResponse.nickname = user.getNickname();
userResponse.email = user.getEmail();
userResponse.provider = user.getProvider().getName();
userResponse.isDenied = user.getIsDenied();
return userResponse;
}
}
Loading