Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR #761

Merged
merged 4 commits into from
Sep 27, 2023
Merged

PR #761

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,12 @@ public class TripController {
private final CommentService commentService;
private final SseEmitterService sseEmitterService;
private final LoginService loginService;
private final MemberPartyService memberPartyService;


@PostMapping("/trip/create")
public void createTrip(@RequestPart("contentsData") TripCreate tripCreate, @RequestPart("image") MultipartFile uploadFile) throws IOException {
// @RequestParam("title") String title, @RequestParam("capacity") int capacity,
// @RequestParam("closeRecruitDate") String closeRecruitDate,
// @RequestParam("goingDate") String goingDate, @RequestParam("comingDate") String comingDate,
// @RequestParam("area") String area, @RequestParam("sigungu") String sigungu,
// @RequestPart(value = "image", required = false) MultipartFile uploadFile,
// HttpServletRequesrt request) throws IOException {

// 멤버 찾기
// HttpSession session = request.getSession(false);
// String email = (String) session.getAttribute("loginMember");

Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
Member principal = (Member) authentication.getPrincipal();

Expand Down Expand Up @@ -146,6 +138,13 @@ public TripDetail getTripDetail(@PathVariable("id") Long id) {
trip.getStartingDate(), trip.getComingDate(), trip.getContent(),
memberList.size(), memberList, commentList);

Member loggedInMember = loginService.getLoggedInMember();
List<MemberParty> membersInTrip = memberPartyRepository.findAllByPartyId(trip.getParty().getId());
if (!memberPartyService.tripContainsMember(membersInTrip, loggedInMember)) {
tripService.hitTrip(id);
}


return tripDetail;
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/GraduationProject/TripPlannerZ/domain/Trip.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ public class Trip {
private Party party;

private LocalDateTime creationTime;
private Integer likes;
private Integer hits;
private int likes;
@Column(columnDefinition = "integer default 0", nullable = false)
private int hits;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public Page<MemberTrip> tripList(Member member, String sortType, Pageable pageab
List<MemberTrip> content;

switch (sortType) {
case "new2":
case "hits":
content = queryFactory
.select(new QMemberTrip(
trip.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

import GraduationProject.TripPlannerZ.domain.Trip;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Optional;

public interface TripRepository extends JpaRepository<Trip, Long> {

public interface TripRepository extends JpaRepository<Trip, Long>, TripRepositoryCustom {
Optional<Trip> findByUUID(String id);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package GraduationProject.TripPlannerZ.repository;

public interface TripRepositoryCustom {

void updateHits(Long id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package GraduationProject.TripPlannerZ.repository;

import com.querydsl.jpa.impl.JPAQueryFactory;
import jakarta.persistence.EntityManager;

import static GraduationProject.TripPlannerZ.domain.QTrip.trip;

public class TripRepositoryImpl implements TripRepositoryCustom {

private final JPAQueryFactory queryFactory;

public TripRepositoryImpl(EntityManager em) {
this.queryFactory = new JPAQueryFactory(em);
}

@Override
public void updateHits(Long id) {
queryFactory.update(trip)
.set(trip.hits, trip.hits.add(1))
.where(trip.id.eq(id))
.execute();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package GraduationProject.TripPlannerZ.service;

import GraduationProject.TripPlannerZ.domain.Member;
import GraduationProject.TripPlannerZ.domain.MemberParty;
import GraduationProject.TripPlannerZ.repository.MemberPartyRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class MemberPartyService {

private final MemberPartyRepository memberPartyRepository;

public boolean tripContainsMember(List<MemberParty> memberPartyList, Member member) {

for (MemberParty memberParty : memberPartyList) {
if (memberParty.getMember() == member) return true;
}

return false;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public Long createTrip(Trip trip){
return trip.getId();
}

@Transactional
public void hitTrip(Long tripId) {
tripRepository.updateHits(tripId);
}

@Transactional
public Long deleteTrip(Trip trip) {
tripRepository.delete(trip);
Expand Down
Loading