Skip to content

Commit

Permalink
Merge pull request #153 from UMC-WOWMARKET/feat/MyOrderManage-137
Browse files Browse the repository at this point in the history
[feat] 수요조사 등록폼 수정, 등록폼 receiveAddress 추가, 유저 role 변경
  • Loading branch information
yunji118 authored Jan 8, 2024
2 parents 4c196c8 + 04c090a commit fa36d01
Show file tree
Hide file tree
Showing 13 changed files with 172 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package wowmarket.wow_server.admin.adminAccount.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
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.RestController;
import wowmarket.wow_server.admin.adminAccount.dto.ChangeRoleRequestDto;
import wowmarket.wow_server.admin.adminAccount.service.AdminAccountManagementService;
import wowmarket.wow_server.domain.User;

@RestController
@RequiredArgsConstructor
@RequestMapping("/admin")
public class AdminAccountManagementController {
private final AdminAccountManagementService adminAccountManagementService;

@PutMapping("/role/admin")
public ResponseEntity giveAdminRole(@RequestBody ChangeRoleRequestDto requestDto, @AuthenticationPrincipal User user){
return adminAccountManagementService.giveAdminRole(requestDto, user);
}

@PutMapping("/role/user")
public ResponseEntity giveUserRole(@RequestBody ChangeRoleRequestDto requestDto, @AuthenticationPrincipal User user){
return adminAccountManagementService.giveUserRole(requestDto, user);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package wowmarket.wow_server.admin.adminAccount.dto;

import lombok.Getter;

@Getter
public class ChangeRoleRequestDto {
private String email;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package wowmarket.wow_server.admin.adminAccount.service;

import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import wowmarket.wow_server.admin.adminAccount.dto.ChangeRoleRequestDto;
import wowmarket.wow_server.domain.Role;
import wowmarket.wow_server.domain.User;
import wowmarket.wow_server.repository.UserRepository;

@Service
@RequiredArgsConstructor
public class AdminAccountManagementService {
private final UserRepository userRepository;

@Transactional
public ResponseEntity giveAdminRole(ChangeRoleRequestDto requestDto, User user){
// if (!user.getRole().equals("ROLE_ADMIN")){
// throw new ResponseStatusException(HttpStatus.BAD_REQUEST);
// } 관리자만 변경 가능하도록 (그 전에 admin페이지는 관리자만 접근 가능하도록 설정 필요)
User reqUser = userRepository.findByEmail(requestDto.getEmail()).orElseThrow(()->new IllegalArgumentException("존재하지 않는 email 입니다."));
Role roleAdmin = Role.ROLE_ADMIN;
reqUser.updateUserRole(roleAdmin);
userRepository.save(reqUser);

return new ResponseEntity(HttpStatus.OK);
}

@Transactional
public ResponseEntity giveUserRole(ChangeRoleRequestDto requestDto, User user){
// if (!user.getRole().equals("ROLE_ADMIN")){
// throw new ResponseStatusException(HttpStatus.BAD_REQUEST);
// } 관리자만 변경 가능하도록 (그 전에 admin페이지는 관리자만 접근 가능하도록 설정 필요)
User reqUser = userRepository.findByEmail(requestDto.getEmail()).orElseThrow(()->new IllegalArgumentException("존재하지 않는 email 입니다."));
Role roleUser = Role.ROLE_USER;
reqUser.updateUserRole(roleUser);
userRepository.save(reqUser);

return new ResponseEntity(HttpStatus.OK);
}
}
8 changes: 8 additions & 0 deletions src/main/java/wowmarket/wow_server/domain/DemandItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.ColumnDefault;
import wowmarket.wow_server.mypage.myproject.MyDemandProject.dto.MyDemandItemDto;

@AllArgsConstructor
@NoArgsConstructor
Expand Down Expand Up @@ -34,4 +35,11 @@ public class DemandItem extends BaseEntity{
public void setDemandProject(DemandProject demandProject){
this.demandProject=demandProject;
}

public void modify(MyDemandItemDto itemDto){
this.name = itemDto.getName();
this.price = itemDto.getPrice();
this.goal = itemDto.getGoal();
this.limits = itemDto.getLimits();
}
}
16 changes: 16 additions & 0 deletions src/main/java/wowmarket/wow_server/domain/DemandProject.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import jakarta.persistence.*;
import lombok.*;
import wowmarket.wow_server.mypage.myproject.MyDemandProject.dto.MyDemandProjectModifyRequestDto;

import java.time.LocalDate;
import java.time.LocalDateTime;
Expand Down Expand Up @@ -62,4 +63,19 @@ public void setCategory(Category category){
this.category = category;
}

public void modify(MyDemandProjectModifyRequestDto requestDto, Category category){
this.projectName = requestDto.getProjectName();
this.description = requestDto.getDescription();
this.sellerName = requestDto.getSellerName();
this.phoneNumber = requestDto.getSellerPhoneNumber();
this.email = requestDto.getSellerEmail();
this.sellerEtc = requestDto.getSellerEtc();
this.thumbnail = requestDto.getThumbnail();
this.image1 = requestDto.getImage1();
this.image2 = requestDto.getImage2();
this.image3 = requestDto.getImage3();
this.startDate = requestDto.getStartDate();
this.endDate = requestDto.getEndDate();
this.category = category;
}
}
3 changes: 3 additions & 0 deletions src/main/java/wowmarket/wow_server/domain/Role.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package wowmarket.wow_server.domain;

import lombok.Builder;

public enum Role {
ROLE_USER, ROLE_ADMIN;

}
5 changes: 5 additions & 0 deletions src/main/java/wowmarket/wow_server/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public class User extends BaseEntity implements UserDetails {
@Column(columnDefinition = "integer default 0", nullable = false)
private int demandLike;

public void updateUserRole(Role role){
this.role = role;
}

public void encodePassword(PasswordEncoder passwordEncoder) {
this.password = passwordEncoder.encode(password);
}
Expand Down Expand Up @@ -103,6 +107,7 @@ public boolean isEnabled() {
}



/////***
// //== 회원탈퇴 -> 작성한 게시물, 댓글 모두 삭제 ==//
// @OneToMany(mappedBy = "writer", cascade = ALL, orphanRemoval = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.web.bind.annotation.*;
import wowmarket.wow_server.domain.User;
import wowmarket.wow_server.mypage.myproject.MyDemandProject.dto.MyDemandDetailResponseDto;
import wowmarket.wow_server.mypage.myproject.MyDemandProject.dto.MyDemandProjectModifyRequestDto;
import wowmarket.wow_server.mypage.myproject.MyDemandProject.dto.MyDemandResponseDto;
import wowmarket.wow_server.mypage.myproject.MyDemandProject.service.MyDemandProjectService;

Expand Down Expand Up @@ -37,5 +38,9 @@ public MyDemandDetailResponseDto getMyDemandDetailForm(@PathVariable Long demand
}


@PutMapping("/{demand_project_id}/modify")
public ResponseEntity modifyMyDemandProject(@PathVariable Long demand_project_id, @RequestBody MyDemandProjectModifyRequestDto requestDto, @AuthenticationPrincipal User user){
return myDemandProjectService.modifyMyDemandProject(demand_project_id, requestDto, user);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package wowmarket.wow_server.mypage.myproject.MyDemandProject.dto;

import lombok.Getter;

import java.time.LocalDateTime;
import java.util.List;

@Getter
public class MyDemandProjectModifyRequestDto {
private String projectName;
private String description;
private String sellerName;
private String sellerPhoneNumber;
private String sellerEmail;
private String sellerEtc;
private String thumbnail;
private Long categoryId;
private String image1;
private String image2;
private String image3;
private List<MyDemandItemDto> itemList;
private LocalDateTime startDate;
private LocalDateTime endDate;

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,10 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.server.ResponseStatusException;
import wowmarket.wow_server.domain.DemandItem;
import wowmarket.wow_server.domain.DemandProject;
import wowmarket.wow_server.domain.User;
import wowmarket.wow_server.domain.*;
import wowmarket.wow_server.global.jwt.SecurityUtil;
import wowmarket.wow_server.mypage.myproject.MyDemandProject.dto.MyDemandDetailResponseDto;
import wowmarket.wow_server.mypage.myproject.MyDemandProject.dto.MyDemandDto;
import wowmarket.wow_server.mypage.myproject.MyDemandProject.dto.MyDemandItemDto;
import wowmarket.wow_server.mypage.myproject.MyDemandProject.dto.MyDemandResponseDto;
import wowmarket.wow_server.mypage.myproject.MyDemandProject.dto.*;
import wowmarket.wow_server.repository.CategoryRepository;
import wowmarket.wow_server.repository.DemandItemRepository;
import wowmarket.wow_server.repository.DemandProjectRepository;
import wowmarket.wow_server.repository.UserRepository;
Expand All @@ -28,7 +24,7 @@
public class MyDemandProjectService {
private final DemandProjectRepository demandProjectRepository;
private final DemandItemRepository demandItemRepository;
private final UserRepository userRepository;
private final CategoryRepository categoryRepository;

@Transactional(readOnly = true)
public MyDemandResponseDto findAllMyDemandForm(Pageable pageable, User user){
Expand Down Expand Up @@ -60,4 +56,25 @@ public MyDemandDetailResponseDto findMyDemandFormDetail(Long demand_project_id,
return responseDto;
}

@Transactional
public ResponseEntity modifyMyDemandProject(Long projectId, MyDemandProjectModifyRequestDto requestDto, User user){
DemandProject demandProject = demandProjectRepository.findById(projectId).orElseThrow(() -> new IllegalArgumentException("해당 demand project id가 없습니다."));
if (user == null || user.getId() != demandProject.getUser().getId()){
throw new ResponseStatusException(HttpStatus.BAD_REQUEST);
}
Category category = categoryRepository.findById(requestDto.getCategoryId()).orElseThrow(() -> new IllegalArgumentException("해당 category id가 없습니다."));
demandProject.modify(requestDto, category);

for(int i=0;i<requestDto.getItemList().size();i++){
MyDemandItemDto itemDto = requestDto.getItemList().get(i);
DemandItem demandItem = demandItemRepository.findDemandItemById(itemDto.getItemId());
demandItem.modify(itemDto);
demandItemRepository.save(demandItem);
}

demandProjectRepository.save(demandProject);
return new ResponseEntity(HttpStatus.OK);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class MySalesDetailResponseDto {
private LocalDateTime startDate;
private LocalDateTime endDate;
private String receiveType;
private String receiveAddress;
private String sellerBank;
private String sellerAccount;
private String sellerAccountName;
Expand All @@ -46,6 +47,7 @@ public MySalesDetailResponseDto(Project project, List<MySalesItemDto> itemDtos){
this.startDate = project.getStartDate();
this.endDate = project.getEndDate();
this.receiveType = project.getReceive_type().toString();
this.receiveAddress = project.getReceive_address();
this.sellerBank = project.getBank();
this.sellerAccount = project.getAccount();
this.sellerAccountName = project.getAccount_holder_name();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,4 @@ public class MySalesProjectModifyRequestDto {
private String sellerEmail;
private String sellerEtc;

@Builder
public MySalesProjectModifyRequestDto(String projectName, String description, String sellerName, String phoneNumber, String email, String sellerEtc,
Long categoryId, String thumbnail, String image1, String image2, String image3, LocalDateTime startDate,
LocalDateTime endDate, Long receiveType, String receiveAddress, Long deliveryFee, String bank, String account,
String accountHolderName, List<MySalesItemDto> itemList){
this.projectName = projectName;
this.description = description;
this.sellerName = sellerName;
this.sellerPhoneNumber = phoneNumber;
this.sellerEmail = email;
this.sellerEtc = sellerEtc;
this.categoryId = categoryId;
this.thumbnail = thumbnail;
this.image1 = image1;
this.image2 = image2;
this.image3 = image3;
this.startDate = startDate;
this.endDate = endDate;
this.receiveType = receiveType;
this.receiveAddress = receiveAddress;
this.deliveryFee = deliveryFee;
this.sellerBank = bank;
this.sellerAccount = account;
this.sellerAccountName = accountHolderName;
this.itemList = itemList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public MySalesDetailResponseDto findMySalesDetail(Long project_id){

@Transactional
public ResponseEntity modifyMySalesProject(Long projectId, MySalesProjectModifyRequestDto requestDto, User user){
Project project = projectRepository.findById(projectId).orElseThrow(()->new IllegalArgumentException("해당 project id가 없습니다."));
Project project = projectRepository.findById(projectId).orElseThrow(()->new IllegalArgumentException("해당 sales project id가 없습니다."));
if (user == null || project.getUser().getId() != user.getId()){
throw new ResponseStatusException(HttpStatus.BAD_REQUEST);
}
Expand Down

0 comments on commit fa36d01

Please sign in to comment.