Skip to content

Commit

Permalink
Merge pull request #10 from KNUCD/feat/issue-1/complaint
Browse files Browse the repository at this point in the history
Feat: Complaint Entity, Repository, Service 추가
  • Loading branch information
dkdud9261 authored Sep 21, 2022
2 parents 253a3e2 + 05580ee commit 2f67032
Show file tree
Hide file tree
Showing 10 changed files with 262 additions and 0 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ dependencies {
runtimeOnly 'com.h2database:h2'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.springframework.boot:spring-boot-starter-validation'
}

tasks.named('test') {
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/server/knucd/complaint/dto/CreateComplaintForm.java
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;
}
4 changes: 4 additions & 0 deletions src/main/java/server/knucd/complaint/entity/Category.java
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 src/main/java/server/knucd/complaint/entity/Complaint.java
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 src/main/java/server/knucd/complaint/entity/Expression.java
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;
}
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
}
14 changes: 14 additions & 0 deletions src/main/java/server/knucd/complaint/entity/Location.java
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;
}
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 src/main/java/server/knucd/complaint/service/ComplaintService.java
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);
}

}
33 changes: 33 additions & 0 deletions src/main/java/server/knucd/member/entity/Member.java
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;
}
}

0 comments on commit 2f67032

Please sign in to comment.