Skip to content

Commit

Permalink
Yv3 #2/like (#24)
Browse files Browse the repository at this point in the history
* feat[v2#2]: Item funding quantity 구현 중

* chore[v2#2]: MemberCreateRequest 이름 수정

* feat[v2#2]: funding 구현 중

* order, item관계 설정

* item, order 관계 설정

* fix[#1]: order반환 실수 수정

* feat[#2]: 좋아요 기능 구현
  • Loading branch information
yujin9747 authored Jan 27, 2023
1 parent 75514ec commit d44dbcd
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 3 deletions.
17 changes: 17 additions & 0 deletions src/main/java/com/noriton/team9/controller/SampleController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.noriton.team9.controller;

import com.noriton.team9.request.LikeRequest;
import com.noriton.team9.request.SampleCreationRequest;
import com.noriton.team9.service.SampleService;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -28,4 +29,20 @@ public ResponseEntity readSample(@PathVariable Long sampleId){
public ResponseEntity createSample(@RequestBody SampleCreationRequest request){
return ResponseEntity.ok(sampleService.createSample(request));
}

@PostMapping("/sample/like")
public ResponseEntity likeSample(@RequestBody LikeRequest request){
return ResponseEntity.ok(sampleService.likeSample(request));
}

@PostMapping("/sample/unlike")
public ResponseEntity unlikeSample(@RequestBody LikeRequest request){
return ResponseEntity.ok(sampleService.unlikeSample(request));
}

@GetMapping("/sample/desc")
public ResponseEntity readSamplesByLike(){
return ResponseEntity.ok(sampleService.readSamplesByLike());
}

}
3 changes: 3 additions & 0 deletions src/main/java/com/noriton/team9/domain/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public class Member {
@OneToMany(mappedBy = "member")
private List<Orders> ordersList = new ArrayList<>();

// @ManyToMany(mappedBy = "likedMembers")
// private List<Sample> likedSamples = new ArrayList<>();

public static Member createMember(String loginId, String password){
Member member = new Member();
member.setLoginId(loginId);
Expand Down
19 changes: 18 additions & 1 deletion src/main/java/com/noriton/team9/domain/Sample.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;

@Getter
@Setter
@Entity
@Table(name = "sample")
public class Sample {
public class Sample{

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand All @@ -22,6 +25,10 @@ public class Sample {
private String imageUrl;
private String content;

// @ManyToMany(mappedBy = "likedSamples")
// private List<Member> likedMembers = new ArrayList<>();

private int likeCount;

@ManyToOne
@JoinColumn(name = "designer_id")
Expand All @@ -32,4 +39,14 @@ public void settingDesigner(Designer designer) {
this.designer = designer;
designer.getSamples().add(this);
}

// @Override
// public int compareTo(Sample s) {
// if (s.likeCount < likeCount) {
// return 1;
// } else if (s.likeCount > likeCount) {
// return -1;
// }
// return 0;
// }
}
26 changes: 26 additions & 0 deletions src/main/java/com/noriton/team9/domain/SampleLike.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.noriton.team9.domain;

import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Entity
@Getter
@Setter
public class SampleLike {

@Id @GeneratedValue
@Column(name = "sampleLike_id")
private Long id;

@ManyToOne
@JoinColumn(name = "sample_id")
private Sample sample;

@ManyToOne
@JoinColumn(name = "member_id")
private Member member;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.noriton.team9.repository;

import com.noriton.team9.domain.Orders;
import com.noriton.team9.domain.SampleLike;
import jakarta.persistence.EntityManager;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
@RequiredArgsConstructor
public class SampleLikeRepository {

private final EntityManager em;
public SampleLike save(SampleLike sampleLike) {
em.persist(sampleLike);
return sampleLike;
}

public List<SampleLike> findBySampleId(Long sampleId){
return em.createQuery("select s from SampleLike s where s.sample.id = :id")
.setParameter("id", sampleId)
.getResultList();
}

public void deleteOne(Long sampleId, Long memberId) {
List<SampleLike> result = em.createQuery("delete from SampleLike s where s.sample.id = :sampleId and s.member.id = :memberId")
.setParameter("sampleId", sampleId)
.setParameter("memberId", memberId)
.getResultList();
em.remove(result.get(0));
}
}
10 changes: 10 additions & 0 deletions src/main/java/com/noriton/team9/request/LikeRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.noriton.team9.request;

import lombok.Data;

@Data
public class LikeRequest {

private Long memberId;
private Long sampleId;
}
50 changes: 48 additions & 2 deletions src/main/java/com/noriton/team9/service/SampleService.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.noriton.team9.service;

import com.noriton.team9.domain.Designer;
import com.noriton.team9.domain.Sample;
import com.noriton.team9.domain.*;
import com.noriton.team9.repository.DesignerRepository;
import com.noriton.team9.repository.MemberRepository;
import com.noriton.team9.repository.SampleLikeRepository;
import com.noriton.team9.repository.SampleRepository;
import com.noriton.team9.request.LikeRequest;
import com.noriton.team9.request.SampleCreationRequest;
import jakarta.persistence.EntityNotFoundException;
import lombok.RequiredArgsConstructor;
Expand All @@ -19,6 +21,8 @@
public class SampleService {
public final SampleRepository sampleRepository;
public final DesignerRepository designerRepository;
public final MemberRepository memberRepository;
private final SampleLikeRepository sampleLikeRepository;

public List<Sample> readSamples(){
return sampleRepository.findAll();
Expand All @@ -43,6 +47,7 @@ public Sample createSample(SampleCreationRequest sample){
Sample sampleToCreate = new Sample();
BeanUtils.copyProperties(sample, sampleToCreate);
sampleToCreate.settingDesigner(designer.get());
// sampleToCreate.setLikeCount(0);
return sampleRepository.save(sampleToCreate);
}

Expand All @@ -53,4 +58,45 @@ public Sample createSample(SampleCreationRequest sample){
public void deleteSample(Long sampleId){
sampleRepository.deleteById(sampleId);
}

/**
* 샘플 좋아요 누르기
* */
@Transactional
public Sample likeSample(LikeRequest request) {
Member member = memberRepository.findOne(request.getMemberId());
Optional<Sample> getSample = sampleRepository.findById(request.getSampleId());
Sample sample = getSample.get();

// sample.getLikedMembers().add(member);
// sample.setLikeCount(sample.getLikeCount() + 1);
// member.getLikedSamples().add(sample);
// memberRepository.save(member);
SampleLike sampleLike = new SampleLike();
sampleLike.setMember(member);
sampleLike.setSample(sample);
sampleLikeRepository.save(sampleLike);

List<SampleLike> likeList = sampleLikeRepository.findBySampleId(request.getSampleId());
sample.setLikeCount(likeList.size());
return sample;
}

public Sample unlikeSample(LikeRequest request) {
Optional<Sample> getSample = sampleRepository.findById(request.getSampleId());
Sample sample = getSample.get();

List<SampleLike> likeList = sampleLikeRepository.findBySampleId(request.getSampleId());
sample.setLikeCount(likeList.size());

sampleLikeRepository.deleteOne(request.getSampleId(), request.getMemberId());
sample.setLikeCount(sample.getLikeCount() - 1);
return sample;
}

public List<Sample> readSamplesByLike() {
List<Sample> list = sampleRepository.findAll();
// Collections.sort(list);
return list;
}
}

0 comments on commit d44dbcd

Please sign in to comment.