-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from AR-TTUBEOG/feature/2
[Feat] Benefit 생성, 수정, 삭제 API
- Loading branch information
Showing
8 changed files
with
316 additions
and
5 deletions.
There are no files selected for viewing
109 changes: 109 additions & 0 deletions
109
src/main/java/com/ttubeog/domain/benefit/application/BenefitService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,120 @@ | ||
package com.ttubeog.domain.benefit.application; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.ttubeog.domain.benefit.domain.Benefit; | ||
import com.ttubeog.domain.benefit.domain.repository.BenefitRepository; | ||
import com.ttubeog.domain.benefit.dto.request.CreateBenefitReq; | ||
import com.ttubeog.domain.benefit.dto.request.UpdateBenefitReq; | ||
import com.ttubeog.domain.benefit.dto.response.CreateBenefitRes; | ||
import com.ttubeog.domain.benefit.dto.response.UpdateBenefitRes; | ||
import com.ttubeog.domain.member.domain.Member; | ||
import com.ttubeog.domain.member.domain.repository.MemberRepository; | ||
import com.ttubeog.domain.store.domain.Store; | ||
import com.ttubeog.domain.store.domain.repository.StoreRepository; | ||
import com.ttubeog.global.DefaultAssert; | ||
import com.ttubeog.global.config.security.token.UserPrincipal; | ||
import com.ttubeog.global.payload.ApiResponse; | ||
import com.ttubeog.global.payload.Message; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.Optional; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
@Transactional(readOnly = true) | ||
public class BenefitService { | ||
|
||
private final MemberRepository memberRepository; | ||
private final BenefitRepository benefitRepository; | ||
private final StoreRepository storeRepository; | ||
|
||
// 혜택 생성 | ||
@Transactional | ||
public ResponseEntity<?> createBenefit(UserPrincipal userPrincipal, CreateBenefitReq createBenefitReq) throws JsonProcessingException { | ||
|
||
Optional<Member> memberOptional = memberRepository.findById(userPrincipal.getId()); | ||
DefaultAssert.isOptionalPresent(memberOptional); | ||
|
||
// Optional<Store> storeOptional = storeRepository.findById(createBenefitReq.getStoreId()); | ||
// Store store; | ||
// DefaultAssert.isOptionalPresent(storeOptional); | ||
// store = storeOptional.get(); | ||
|
||
Benefit benefit = Benefit.builder() | ||
.content(createBenefitReq.getContent()) | ||
.type(createBenefitReq.getType()) | ||
// .store(store) | ||
.build(); | ||
|
||
benefitRepository.save(benefit); | ||
|
||
CreateBenefitRes createBenefitRes = CreateBenefitRes.builder() | ||
.benefitId(benefit.getId()) | ||
// .storeId(benefit.getStore().getId()) | ||
.content(benefit.getContent()) | ||
.type(benefit.getType()) | ||
.build(); | ||
|
||
ApiResponse apiResponse = ApiResponse.builder() | ||
.check(true) | ||
.information(createBenefitRes) | ||
.build(); | ||
|
||
return ResponseEntity.ok(apiResponse); | ||
} | ||
|
||
// 혜택 삭제 | ||
@Transactional | ||
public ResponseEntity<?> deleteBenefit(UserPrincipal userPrincipal, Long benefitId) throws JsonProcessingException { | ||
|
||
Optional<Member> userOptional = memberRepository.findById(userPrincipal.getId()); | ||
DefaultAssert.isOptionalPresent(userOptional); | ||
|
||
Optional<Benefit> benefitOptional = benefitRepository.findById(benefitId); | ||
DefaultAssert.isTrue(benefitOptional.isPresent(), "존재하지 않는 혜택입니다."); | ||
Benefit benefit = benefitOptional.get(); | ||
|
||
benefitRepository.delete(benefit); | ||
|
||
ApiResponse apiResponse = ApiResponse.builder() | ||
.check(true) | ||
.information(Message.builder().message("혜택을 삭제했습니다.").build()) | ||
.build(); | ||
|
||
return ResponseEntity.ok(apiResponse); | ||
} | ||
|
||
//혜택 수정 | ||
@Transactional | ||
public ResponseEntity<?> updateBenefit(UserPrincipal userPrincipal, UpdateBenefitReq updateBenefitReq) throws JsonProcessingException { | ||
|
||
Optional<Member> userOptional = memberRepository.findById(userPrincipal.getId()); | ||
DefaultAssert.isOptionalPresent(userOptional); | ||
|
||
Optional<Benefit> benefitOptional = benefitRepository.findById(updateBenefitReq.getBenefitId()); | ||
DefaultAssert.isTrue(benefitOptional.isPresent(), "존재하지 않는 혜택입니다."); | ||
Benefit benefit = benefitOptional.get(); | ||
|
||
//TODO userOptional과 benefit의 userId가 일치하는지 확인 | ||
|
||
benefit.updateContent(updateBenefitReq.getContent()); | ||
|
||
UpdateBenefitRes updateBenefitRes = UpdateBenefitRes.builder() | ||
.benefitId(benefit.getId()) | ||
//.storeId(benefit.getStore().getId()) | ||
.content(benefit.getContent()) | ||
.type(benefit.getType()) | ||
.build(); | ||
|
||
ApiResponse apiResponse = ApiResponse.builder() | ||
.check(true) | ||
.information(updateBenefitRes) | ||
.build(); | ||
|
||
return ResponseEntity.ok(apiResponse); | ||
} | ||
|
||
} |
33 changes: 29 additions & 4 deletions
33
src/main/java/com/ttubeog/domain/benefit/domain/Benefit.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,40 @@ | ||
package com.ttubeog.domain.benefit.domain; | ||
|
||
import com.ttubeog.domain.common.BaseEntity; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import com.ttubeog.domain.store.domain.Store; | ||
import jakarta.persistence.*; | ||
import lombok.*; | ||
|
||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@Getter | ||
@Entity | ||
@Table(name = "benefit") | ||
public class Benefit extends BaseEntity { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
@Column(name = "content") | ||
private String content; | ||
|
||
@Column(name = "type") | ||
@Enumerated(EnumType.STRING) | ||
private BenefitType type; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "store_id") | ||
private Store store; | ||
|
||
@Builder | ||
public Benefit(Long id, String content, BenefitType type, Store store) { | ||
this.id = id; | ||
this.content = content; | ||
this.type = type; | ||
this.store = store; | ||
} | ||
|
||
public void updateContent(String content) { | ||
this.content = content; | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/com/ttubeog/domain/benefit/domain/BenefitType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.ttubeog.domain.benefit.domain; | ||
|
||
public enum BenefitType { | ||
sale, plus, gift | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/com/ttubeog/domain/benefit/dto/request/CreateBenefitReq.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.ttubeog.domain.benefit.dto.request; | ||
|
||
import com.ttubeog.domain.benefit.domain.BenefitType; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import lombok.Data; | ||
|
||
import java.math.BigInteger; | ||
|
||
@Data | ||
@Schema(description = "CreateBenefitRequest") | ||
public class CreateBenefitReq { | ||
|
||
// @Schema(description = "매장 ID", example = "1") | ||
// private Long storeId; | ||
|
||
@Schema(description = "내용", example = "아메리카노 20% 할인") | ||
private String content; | ||
|
||
@Schema(description = "혜택타입", example = "sale") | ||
private BenefitType type; | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/ttubeog/domain/benefit/dto/request/UpdateBenefitReq.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.ttubeog.domain.benefit.dto.request; | ||
|
||
import com.ttubeog.domain.benefit.domain.BenefitType; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import lombok.Data; | ||
|
||
@Data | ||
@Schema(description = "UpdateBenefitRequest") | ||
public class UpdateBenefitReq { | ||
|
||
@Schema(description = "혜택 ID", example = "1") | ||
private Long benefitId; | ||
|
||
@Schema(description = "내용", example = "아메리카노 20% 할인") | ||
private String content; | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/com/ttubeog/domain/benefit/dto/response/CreateBenefitRes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.ttubeog.domain.benefit.dto.response; | ||
|
||
import com.ttubeog.domain.benefit.domain.BenefitType; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
|
||
@Data | ||
public class CreateBenefitRes { | ||
|
||
@Schema(description = "혜택 ID", example = "1") | ||
private Long benefitId; | ||
|
||
// @Schema(description = "매장 ID", example = "1") | ||
// private Long storeId; | ||
|
||
@Schema(description = "내용", example = "아메리카노 20% 할인") | ||
private String content; | ||
|
||
@Schema(description = "혜택타입", example = "sale") | ||
private BenefitType type; | ||
|
||
@Builder | ||
// public CreateBenefitRes(Long benefitId, Long storeId, String content, BenefitType type) { | ||
public CreateBenefitRes(Long benefitId, String content, BenefitType type) { | ||
this.benefitId = benefitId; | ||
// this.storeId = storeId; | ||
this.content = content; | ||
this.type = type; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/com/ttubeog/domain/benefit/dto/response/UpdateBenefitRes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.ttubeog.domain.benefit.dto.response; | ||
|
||
import com.ttubeog.domain.benefit.domain.BenefitType; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
|
||
@Data | ||
public class UpdateBenefitRes { | ||
|
||
@Schema(description = "혜택 ID", example = "1") | ||
private Long benefitId; | ||
|
||
// @Schema(description = "매장 ID", example = "1") | ||
// private Long storeId; | ||
|
||
@Schema(description = "내용", example = "아메리카노 20% 할인") | ||
private String content; | ||
|
||
@Schema(description = "혜택타입", example = "sale") | ||
private BenefitType type; | ||
|
||
@Builder | ||
// public CreateBenefitRes(Long benefitId, Long storeId, String content, BenefitType type) { | ||
public UpdateBenefitRes(Long benefitId, String content, BenefitType type) { | ||
this.benefitId = benefitId; | ||
// this.storeId = storeId; | ||
this.content = content; | ||
this.type = type; | ||
} | ||
|
||
} |
72 changes: 71 additions & 1 deletion
72
src/main/java/com/ttubeog/domain/benefit/presentation/BenefitController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,77 @@ | ||
package com.ttubeog.domain.benefit.presentation; | ||
|
||
import org.springframework.web.bind.annotation.RestController; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.ttubeog.domain.benefit.application.BenefitService; | ||
import com.ttubeog.domain.benefit.dto.request.CreateBenefitReq; | ||
import com.ttubeog.domain.benefit.dto.request.UpdateBenefitReq; | ||
import com.ttubeog.domain.benefit.dto.response.CreateBenefitRes; | ||
import com.ttubeog.domain.benefit.dto.response.UpdateBenefitRes; | ||
import com.ttubeog.global.config.security.token.CurrentUser; | ||
import com.ttubeog.global.config.security.token.UserPrincipal; | ||
import com.ttubeog.global.payload.ErrorResponse; | ||
import com.ttubeog.global.payload.Message; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.Parameter; | ||
import io.swagger.v3.oas.annotations.media.Content; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponses; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import jakarta.validation.Valid; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@Tag(name = "Benefit", description = "Benefit API") | ||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api/v1/benefit") | ||
public class BenefitController { | ||
|
||
private final BenefitService benefitService; | ||
|
||
//혜택 생성 | ||
@Operation(summary = "혜택 생성", description = "매장의 혜택을 생성합니다.") | ||
@ApiResponses(value = { | ||
@ApiResponse(responseCode = "200", description = "혜택 생성 성공", content = { @Content(mediaType = "application/json", schema = @Schema(implementation = CreateBenefitRes.class) ) } ), | ||
@ApiResponse(responseCode = "400", description = "혜택 생성 실패", content = { @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class) ) } ), | ||
}) | ||
@PostMapping | ||
public ResponseEntity<?> createBenefit( | ||
@Parameter(description = "Accesstoken을 입력해주세요.", required = true) | ||
@CurrentUser UserPrincipal userPrincipal, | ||
@Valid @RequestBody CreateBenefitReq createBenefitReq | ||
) throws JsonProcessingException { | ||
return benefitService.createBenefit(userPrincipal, createBenefitReq); | ||
} | ||
|
||
//혜택 삭제 | ||
@Operation(summary = "혜택 삭제", description = "매장의 혜택을 삭제합니다.") | ||
@ApiResponses(value = { | ||
@ApiResponse(responseCode = "200", description = "혜택 삭제 성공", content = { @Content(mediaType = "application/json", schema = @Schema(implementation = Message.class) ) } ), | ||
@ApiResponse(responseCode = "400", description = "혜택 삭제 실패", content = { @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class) ) } ), | ||
}) | ||
@DeleteMapping("/{benefitId}") | ||
public ResponseEntity<?> deleteBenefit( | ||
@Parameter(description = "Accesstoken을 입력해주세요.", required = true) | ||
@CurrentUser UserPrincipal userPrincipal, | ||
@PathVariable(value = "benefitId") Long benefitId | ||
) throws JsonProcessingException { | ||
return benefitService.deleteBenefit(userPrincipal, benefitId); | ||
} | ||
|
||
//혜택 수정 | ||
@Operation(summary = "혜택 수정", description = "매장의 혜택을 수정합니다.") | ||
@ApiResponses(value = { | ||
@ApiResponse(responseCode = "200", description = "혜택 수정 성공", content = { @Content(mediaType = "application/json", schema = @Schema(implementation = UpdateBenefitRes.class) ) } ), | ||
@ApiResponse(responseCode = "400", description = "혜택 수정 실패", content = { @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class) ) } ), | ||
}) | ||
@PatchMapping | ||
public ResponseEntity<?> updateBenefit( | ||
@Parameter(description = "Accesstoken을 입력해주세요.", required = true) | ||
@CurrentUser UserPrincipal userPrincipal, | ||
@Valid @RequestBody UpdateBenefitReq updateBenefitReq | ||
) throws JsonProcessingException { | ||
return benefitService.updateBenefit(userPrincipal, updateBenefitReq); | ||
} | ||
} |