Skip to content

Commit

Permalink
Merge branch 'feat/buyornot' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
wwingyou committed Jan 27, 2025
2 parents acd2477 + 4d1c9b4 commit 5b9415e
Show file tree
Hide file tree
Showing 13 changed files with 571 additions and 33 deletions.
54 changes: 54 additions & 0 deletions src/main/java/com/goolbitg/api/controller/BuyOrNotController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,74 @@

import java.util.Optional;

import jakarta.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.NativeWebRequest;

import com.goolbitg.api.BuyOrNotApi;
import com.goolbitg.api.model.BuyOrNotDto;
import com.goolbitg.api.model.BuyOrNotVoteChangeDto;
import com.goolbitg.api.model.BuyOrNotVoteDto;
import com.goolbitg.api.model.PaginatedBuyOrNotDto;
import com.goolbitg.api.service.BuyOrNotService;

import lombok.RequiredArgsConstructor;

/**
* BuyOrNotController
*/
@RestController
@RequiredArgsConstructor
public class BuyOrNotController implements BuyOrNotApi {

@Autowired
private final BuyOrNotService buyOrNotService;

@Override
public Optional<NativeWebRequest> getRequest() {
return ControllerUtils.getRequest();
}

@Override
public ResponseEntity<BuyOrNotDto> createBuyOrNot(@Valid BuyOrNotDto buyOrNotDto) throws Exception {
BuyOrNotDto result = buyOrNotService.createBuyOrNot(buyOrNotDto);
return new ResponseEntity<>(result, HttpStatus.CREATED);
}

@Override
public ResponseEntity<Void> deleteBuyOrNot(Long postId) throws Exception {
buyOrNotService.deleteBuyOrNot(postId);
return ResponseEntity.ok().build();
}

@Override
public ResponseEntity<BuyOrNotDto> getBuyOrNot(Long postId) throws Exception {
BuyOrNotDto result = buyOrNotService.getBuyOrNot(postId);
return ResponseEntity.ok(result);
}

@Override
public ResponseEntity<PaginatedBuyOrNotDto> getBuyOrNots(@Valid Integer page, @Valid Integer size,
@Valid Boolean created) throws Exception {
PaginatedBuyOrNotDto result = buyOrNotService.getBuyOrNots(page, size, created);
return ResponseEntity.ok(result);
}

@Override
public ResponseEntity<BuyOrNotDto> updateBuyOrNot(Long postId, @Valid BuyOrNotDto buyOrNotDto) throws Exception {
BuyOrNotDto result = buyOrNotService.updateBuyOrNot(postId, buyOrNotDto);
return ResponseEntity.ok(result);
}

@Override
public ResponseEntity<BuyOrNotVoteChangeDto> voteBuyOrNot(Long postId, @Valid BuyOrNotVoteDto buyOrNotVoteDto)
throws Exception {
BuyOrNotVoteChangeDto result = buyOrNotService.voteBuyOrNot(postId, buyOrNotVoteDto);
return ResponseEntity.ok(result);
}

}
25 changes: 25 additions & 0 deletions src/main/java/com/goolbitg/api/entity/BaseEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.goolbitg.api.entity;

import java.time.LocalDateTime;

import jakarta.persistence.Column;
import jakarta.persistence.MappedSuperclass;

import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

/**
* BaseEntity
*/
@MappedSuperclass
public abstract class BaseEntity {

@CreationTimestamp
@Column(name = "created_at", updatable = false)
private LocalDateTime createdAt;

@UpdateTimestamp
@Column(name = "updated_at")
private LocalDateTime updatedAt;

}
45 changes: 45 additions & 0 deletions src/main/java/com/goolbitg/api/entity/BuyOrNot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.goolbitg.api.entity;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;

import lombok.Getter;
import lombok.Setter;

/**
* BuyOrNot
*/
@Entity
@Table(name = "buyornots")
@Getter
@Setter
public class BuyOrNot {

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

@Column(name = "writer_id")
private String writerId;

@Column(name = "product_name")
private String productName;

@Column(name = "product_price")
private Integer productPrice;

@Column(name = "product_image_url")
private String productImageUrl;

@Column(name = "good_reason")
private String goodReason;

@Column(name = "bad_reason")
private String badReason;

}
39 changes: 39 additions & 0 deletions src/main/java/com/goolbitg/api/entity/BuyOrNotVote.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.goolbitg.api.entity;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.Id;
import jakarta.persistence.IdClass;
import jakarta.persistence.Table;

import com.goolbitg.api.model.BuyOrNotVoteType;

import lombok.Getter;
import lombok.Setter;

/**
* BuyOrNotVote
*/
@Entity
@Table(name = "buyornot_votes")
@IdClass(BuyOrNotVoteId.class)
@Getter
@Setter
// TODO: extends base entity
public class BuyOrNotVote {

@Id
@Column(name = "post_id")
private Long postId;

@Id
@Column(name = "voter_id")
private String voterId;

@Column(name = "vote")
@Enumerated(EnumType.STRING)
private BuyOrNotVoteType vote;

}
7 changes: 7 additions & 0 deletions src/main/java/com/goolbitg/api/entity/BuyOrNotVoteId.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.goolbitg.api.entity;

/**
* BuyOrNotVoteId
*/
public record BuyOrNotVoteId(Long postId, String voterId) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

public abstract class BuyOrNotException {

public static CommonException postLimitExceed(String id) {
public static CommonException postLimitExceed() {
return new CommonException(
5001,
"포스트 등록 한도를 초과하였습니다.",
HttpStatus.UNPROCESSABLE_ENTITY);
}
public static CommonException postNotExist(String id) {
public static CommonException postNotExist(Long id) {
return new CommonException(
5002,
"포스트가 존재하지 않습니다. (" + id + ")",
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/com/goolbitg/api/repository/BuyOrNotRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.goolbitg.api.repository;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

import com.goolbitg.api.entity.BuyOrNot;

/**
* BuyOrNotRepository
*/
public interface BuyOrNotRepository extends JpaRepository<BuyOrNot, Long> {

Page<BuyOrNot> findAllByWriterId(String loginUserId, Pageable pageReq);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.goolbitg.api.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.goolbitg.api.entity.BuyOrNotVote;
import com.goolbitg.api.entity.BuyOrNotVoteId;
import com.goolbitg.api.model.BuyOrNotVoteType;

/**
* BuyOrNotVoteRepository
*/
public interface BuyOrNotVoteRepository extends JpaRepository<BuyOrNotVote, BuyOrNotVoteId> {

@Query("""
SELECT COUNT(v)
FROM BuyOrNotVote v
WHERE v.postId = :postId
AND v.vote = :vote
""")
Integer getCountOf(@Param("postId") Long postId, @Param("vote") BuyOrNotVoteType vote);

}
20 changes: 20 additions & 0 deletions src/main/java/com/goolbitg/api/service/BuyOrNotService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.goolbitg.api.service;

import com.goolbitg.api.model.BuyOrNotDto;
import com.goolbitg.api.model.BuyOrNotVoteChangeDto;
import com.goolbitg.api.model.BuyOrNotVoteDto;
import com.goolbitg.api.model.PaginatedBuyOrNotDto;

/**
* BuyOrNotService
*/
public interface BuyOrNotService {

BuyOrNotDto getBuyOrNot(Long postId);
PaginatedBuyOrNotDto getBuyOrNots(Integer page, Integer size, Boolean created);
BuyOrNotDto createBuyOrNot(BuyOrNotDto request);
BuyOrNotDto updateBuyOrNot(Long postId, BuyOrNotDto request);
void deleteBuyOrNot(Long postId);
BuyOrNotVoteChangeDto voteBuyOrNot(Long postId, BuyOrNotVoteDto request);

}
Loading

0 comments on commit 5b9415e

Please sign in to comment.