-
Notifications
You must be signed in to change notification settings - Fork 1
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 #10 from KNUCD/feat/issue-1/complaint
Feat: Complaint Entity, Repository, Service 추가
- Loading branch information
Showing
10 changed files
with
262 additions
and
0 deletions.
There are no files selected for viewing
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
30 changes: 30 additions & 0 deletions
30
src/main/java/server/knucd/complaint/dto/CreateComplaintForm.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,30 @@ | ||
package server.knucd.complaint.dto; | ||
|
||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
import org.springframework.web.multipart.MultipartFile; | ||
import server.knucd.complaint.entity.Category; | ||
|
||
import javax.validation.constraints.NotNull; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class CreateComplaintForm { | ||
|
||
@NotNull | ||
private Double latitude; | ||
@NotNull | ||
private Double longitude; | ||
@NotNull | ||
private String title; | ||
@NotNull | ||
private String content; | ||
|
||
private MultipartFile file; | ||
|
||
@NotNull | ||
private Category category; | ||
} |
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,4 @@ | ||
package server.knucd.complaint.entity; | ||
|
||
public enum Category { | ||
} |
48 changes: 48 additions & 0 deletions
48
src/main/java/server/knucd/complaint/entity/Complaint.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,48 @@ | ||
package server.knucd.complaint.entity; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import server.knucd.member.entity.Member; | ||
|
||
import javax.persistence.*; | ||
|
||
@Entity | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@Getter | ||
public class Complaint { | ||
|
||
@Id @GeneratedValue | ||
private Long id; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
private Member writer; | ||
|
||
@Embedded | ||
private Location location; | ||
|
||
private String title; | ||
|
||
private String content; | ||
|
||
private String file; | ||
|
||
@Enumerated(EnumType.STRING) | ||
private Category category; | ||
|
||
@Embedded | ||
private Expression expression; | ||
|
||
@Builder | ||
public Complaint(Member writer, Double latitude, Double longitude, String title, String content, | ||
String file, Category category) { | ||
this.writer = writer; | ||
this.location = new Location(latitude, longitude); | ||
this.title = title; | ||
this.content = content; | ||
this.file = file; | ||
this.category = category; | ||
this.expression = new Expression(0L,0L,0L); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/server/knucd/complaint/entity/Expression.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,18 @@ | ||
package server.knucd.complaint.entity; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import javax.persistence.Embeddable; | ||
|
||
@Embeddable | ||
@Getter | ||
@AllArgsConstructor | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class Expression { | ||
private Long greatCount; | ||
private Long likeCount; | ||
private Long sadCount; | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/server/knucd/complaint/entity/ExpressionType.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 server.knucd.complaint.entity; | ||
|
||
public enum ExpressionType { | ||
GREAT, LIKE, SAD | ||
} |
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,14 @@ | ||
package server.knucd.complaint.entity; | ||
|
||
import lombok.*; | ||
|
||
import javax.persistence.Embeddable; | ||
|
||
@Embeddable | ||
@Getter | ||
@AllArgsConstructor | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class Location { | ||
private Double latitude; | ||
private Double longitude; | ||
} |
49 changes: 49 additions & 0 deletions
49
src/main/java/server/knucd/complaint/repository/ComplaintRepository.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,49 @@ | ||
package server.knucd.complaint.repository; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Repository; | ||
import server.knucd.complaint.entity.Category; | ||
import server.knucd.complaint.entity.Complaint; | ||
|
||
import javax.persistence.EntityManager; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
@Repository | ||
@RequiredArgsConstructor | ||
public class ComplaintRepository { | ||
|
||
private final EntityManager em; | ||
|
||
public void save(Complaint complaint) { | ||
em.persist(complaint); | ||
} | ||
|
||
public Optional<Complaint> findById(Long id) { | ||
return Optional.ofNullable(em.find(Complaint.class, id)); | ||
} | ||
|
||
public List<Complaint> findAllByCategory(Category category) { | ||
return em.createQuery("select c from Complaint c " + | ||
"where c.category = :category " + | ||
"order by c.id desc", | ||
Complaint.class) | ||
.setParameter("category", category) | ||
.getResultList(); | ||
} | ||
|
||
public List<Complaint> findAllByCategory(Category category, int page, int size) { | ||
return em.createQuery("select c from Complaint c " + | ||
"where c.category = :category " + | ||
"order by c.id desc", | ||
Complaint.class) | ||
.setParameter("category", category) | ||
.setMaxResults(size) | ||
.setFirstResult((page-1)*size) | ||
.getResultList(); | ||
} | ||
|
||
public void delete(Complaint complaint) { | ||
em.remove(complaint); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/main/java/server/knucd/complaint/service/ComplaintService.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,60 @@ | ||
package server.knucd.complaint.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import server.knucd.complaint.dto.CreateComplaintForm; | ||
import server.knucd.complaint.entity.Category; | ||
import server.knucd.complaint.entity.Complaint; | ||
import server.knucd.complaint.entity.Location; | ||
import server.knucd.complaint.repository.ComplaintRepository; | ||
import server.knucd.member.entity.Member; | ||
|
||
import java.util.List; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class ComplaintService { | ||
|
||
private final ComplaintRepository complaintRepository; | ||
|
||
public void save(CreateComplaintForm form) { | ||
// sample member | ||
Member member = Member.builder() | ||
.name("테스트") | ||
.phone("010-1234-5678") | ||
.email("[email protected]") | ||
.address("대구 북구 대학로 80") | ||
.build(); | ||
|
||
// 요기에서 파일 스토리지에 저장하고 path 추출 | ||
String path = "/image.png"; | ||
|
||
Complaint complaint = Complaint.builder() | ||
.writer(member) | ||
.title(form.getTitle()) | ||
.content(form.getContent()) | ||
.file(path) | ||
.category(form.getCategory()) | ||
.build(); | ||
|
||
complaintRepository.save(complaint); | ||
} | ||
|
||
public List<Complaint> findAllByCategory(Category category) { | ||
List<Complaint> complaints = complaintRepository.findAllByCategory(category); | ||
return complaints; | ||
} | ||
|
||
public List<Complaint> findAllByCategory(Category category, int page, int size) { | ||
List<Complaint> complaints = complaintRepository.findAllByCategory(category, page, size); | ||
return complaints; | ||
} | ||
|
||
// 감정 표현 추가 기능 구현 예정 | ||
|
||
public void deleteById(Long id) { | ||
Complaint complaint = complaintRepository.findById(id).orElseThrow(); | ||
complaintRepository.delete(complaint); | ||
} | ||
|
||
} |
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,33 @@ | ||
package server.knucd.member.entity; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.NoArgsConstructor; | ||
|
||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.Id; | ||
|
||
@Entity | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class Member { | ||
|
||
@Id @GeneratedValue | ||
private Long id; | ||
|
||
private String name; | ||
|
||
private String phone; | ||
|
||
private String email; | ||
|
||
private String address; | ||
|
||
@Builder | ||
public Member(String name, String phone, String email, String address) { | ||
this.name = name; | ||
this.phone = phone; | ||
this.email = email; | ||
this.address = address; | ||
} | ||
} |