Skip to content

Commit

Permalink
Merge pull request #4 from AR-TTUBEOG/feature/2
Browse files Browse the repository at this point in the history
[Feat] Benefit 생성, 수정, 삭제 API
  • Loading branch information
choeun7 authored Jan 18, 2024
2 parents f55ff38 + 72ddce1 commit e24d4a1
Show file tree
Hide file tree
Showing 8 changed files with 316 additions and 5 deletions.
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 src/main/java/com/ttubeog/domain/benefit/domain/Benefit.java
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;
}
}
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
}
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;

}
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;

}
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;
}
}
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;
}

}
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);
}
}

0 comments on commit e24d4a1

Please sign in to comment.