Skip to content

Commit

Permalink
Merge pull request #72 from Hanaro-trip-together-bank/feature/trip
Browse files Browse the repository at this point in the history
feat: 여행 생성, 삭제, 수정 API
  • Loading branch information
lcw729 authored May 30, 2024
2 parents 9739974 + a99f0c9 commit 11cbcd3
Show file tree
Hide file tree
Showing 7 changed files with 214 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.hanaro.triptogether.trip.controller;

import com.hanaro.triptogether.trip.dto.request.TripReqDto;
import com.hanaro.triptogether.trip.dto.response.TripResDto;
import com.hanaro.triptogether.trip.service.TripService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.NoSuchElementException;

@RestController
@RequiredArgsConstructor
Expand All @@ -25,4 +26,38 @@ public TripResDto getTrip(@PathVariable("trip_idx") Long trip_idx) {
public List<TripResDto> getTrips(@PathVariable("team_idx") Long team_idx) {
return tripService.getTripsByTeam(team_idx);
}

@PostMapping
public ResponseEntity<?> createTrip(@RequestBody TripReqDto reqDto) {
try {
tripService.createTrip(reqDto);

return ResponseEntity.status(HttpStatus.OK).build();
} catch (NoSuchElementException ex) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
}
}

@PutMapping("/{trip_idx}")
public ResponseEntity<?> updateTrip(@PathVariable("trip_idx") Long trip_idx, @RequestBody TripReqDto reqDto) {
try {
tripService.updateTrip(trip_idx, reqDto);

return ResponseEntity.status(HttpStatus.OK).build();
} catch (NoSuchElementException ex) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
}
}

@DeleteMapping("/{trip_idx}")
public ResponseEntity<?> deleteTrip(@PathVariable("trip_idx") Long trip_idx) {
try {
tripService.deleteTrip(trip_idx);

return ResponseEntity.status(HttpStatus.OK).build();
} catch (NoSuchElementException ex) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
}
}

}
53 changes: 47 additions & 6 deletions src/main/java/com/hanaro/triptogether/trip/domain/Trip.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package com.hanaro.triptogether.trip.domain;

import com.hanaro.triptogether.country.domain.CountryEntity;
import com.hanaro.triptogether.member.domain.Member;
import com.hanaro.triptogether.team.domain.Team;
import com.hanaro.triptogether.trip.dto.response.TripResDto;
import com.hanaro.triptogether.tripCity.domain.TripCity;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.*;
import org.hibernate.annotations.CreationTimestamp;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Entity
@Table(name = "trip")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
Expand All @@ -40,9 +40,15 @@ public class Trip {
@Column(nullable = false)
private Integer tripDay = 1;

private Integer tripImg;

private LocalDate tripStartDay;

@OneToMany(mappedBy = "trip", cascade = CascadeType.ALL, orphanRemoval = true)
private List<TripCity> tripCities;

@Column(nullable = false)
@CreationTimestamp
private LocalDateTime createdAt;

@ManyToOne
Expand All @@ -59,4 +65,39 @@ public class Trip {
@ManyToOne
@JoinColumn(name = "deleted_by")
private Member deletedBy;

public TripResDto toTrip() {
return TripResDto.builder()
.teamIdx(this.getTeam().getTeamIdx())
.teamName(this.getTeam().getTeamName())
.tripIdx(this.getTripIdx())
.tripDay(this.getTripDay())
.tripContent(this.getTripContent())
.tripGoalAmount(this.getTripGoalAmount())
.tripName(this.getTripName())
.tripStartDay(this.getTripStartDay())
.build();
}

public void update(String tripName,
String tripContent,
BigDecimal tripGoalAmount,
Integer tripDay,
Integer tripImg,
LocalDate tripStartDay,
List<TripCity> tripCities,
Member member) {
this.tripName = tripName;
this.tripContent = tripContent;
this.tripGoalAmount = tripGoalAmount;
this.tripDay = tripDay;
this.tripImg = tripImg;
this.tripStartDay = tripStartDay;
this.lastModifiedAt = LocalDateTime.now();
this.lastModifiedBy = member;

// 기존 TripCity 목록을 업데이트합니다.
this.tripCities.clear();
this.tripCities.addAll(tripCities);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.hanaro.triptogether.trip.dto.request;


import com.hanaro.triptogether.city.dto.City;
import com.hanaro.triptogether.team.domain.Team;
import com.hanaro.triptogether.trip.domain.Trip;
import lombok.Builder;
import lombok.Getter;
import org.springframework.data.annotation.CreatedBy;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;

@Getter
@Builder
public class TripReqDto {
private Long teamIdx;
private String teamName;
private Long tripIdx;
private String tripName;
private String tripContent;
private BigDecimal tripGoalAmount;
private Integer tripDay;
private Integer tripImg;
private LocalDate tripStartDay;
private List<Long> cities;
private Long createdBy;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class TripResDto {
private String tripContent;
private BigDecimal tripGoalAmount;
private Integer tripDay;
private Integer tripImg;
private LocalDate tripStartDay;
private List<City> cities;
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,41 @@
package com.hanaro.triptogether.trip.service;

import com.hanaro.triptogether.city.domain.CityEntity;
import com.hanaro.triptogether.city.domain.CityRepository;
import com.hanaro.triptogether.city.dto.City;
import com.hanaro.triptogether.country.domain.CountryEntity;
import com.hanaro.triptogether.exception.ApiException;
import com.hanaro.triptogether.exception.ExceptionEnum;
import com.hanaro.triptogether.member.domain.Member;
import com.hanaro.triptogether.member.domain.MemberRepository;
import com.hanaro.triptogether.team.domain.Team;
import com.hanaro.triptogether.team.service.impl.TeamServiceImpl;
import com.hanaro.triptogether.team.domain.TeamRepository;
import com.hanaro.triptogether.trip.domain.Trip;
import com.hanaro.triptogether.trip.domain.TripRepository;
import com.hanaro.triptogether.trip.dto.request.TripReqDto;
import com.hanaro.triptogether.trip.dto.response.TripResDto;
import com.hanaro.triptogether.tripCity.domain.TripCity;
import com.hanaro.triptogether.tripCity.domain.TripCityRepository;
import com.hanaro.triptogether.tripCity.service.TripCityService;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.nio.file.LinkOption;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class TripService {
private final TripRepository tripRepository;
private final TeamRepository teamRepository;
private final CityRepository cityRepository;
private final MemberRepository memberRepository;
private final TripCityService tripCityService;
private final TeamServiceImpl teamService;

Expand All @@ -43,6 +58,77 @@ public List<TripResDto> getTripsByTeam(Long teamIdx) {
return dtos;
}

@Transactional
public void createTrip(TripReqDto tripReqDto) throws NoSuchElementException{
Team team = teamRepository.findById(tripReqDto.getTeamIdx())
.orElseThrow(() -> new NoSuchElementException("Team not found"));

Member member = memberRepository.findById(tripReqDto.getCreatedBy())
.orElseThrow(() -> new NoSuchElementException("Member not found"));

Trip trip = Trip.builder()
.team(team)
.tripName(tripReqDto.getTripName())
.tripContent(tripReqDto.toString())
.tripGoalAmount(tripReqDto.getTripGoalAmount())
.tripDay(tripReqDto.getTripDay())
.tripImg(tripReqDto.getTripImg())
.tripStartDay(tripReqDto.getTripStartDay())
.createdBy(member)
.build();

tripRepository.save(trip);

List<Long> cities = tripReqDto.getCities();
List<TripCity> tripCities = cities.stream().map(cityId -> {
CityEntity entity = cityRepository.findById(cityId)
.orElseThrow(() -> new NoSuchElementException("City not found"));

return TripCity.builder()
.city(entity)
.trip(trip)
.build();
}).collect(Collectors.toList());

trip.setTripCities(tripCities);
}

@Transactional
public void updateTrip(Long tripIdx, TripReqDto tripReqDto) throws NoSuchElementException {

Trip trip = tripRepository.findById(tripIdx)
.orElseThrow(() -> new NoSuchElementException("Trip not found"));

Member member = memberRepository.findById(tripReqDto.getCreatedBy())
.orElseThrow(() -> new NoSuchElementException("Member not found"));

List<TripCity> newTripCities = tripReqDto.getCities().stream().map(cityId -> {
CityEntity entity = cityRepository.findById(cityId)
.orElseThrow(() -> new NoSuchElementException("City not found"));

return TripCity.builder()
.city(entity)
.trip(trip)
.build();
}).collect(Collectors.toList());

trip.update(tripReqDto.getTripName(),
tripReqDto.getTripContent(),
tripReqDto.getTripGoalAmount(),
tripReqDto.getTripDay(),
tripReqDto.getTripImg(),
tripReqDto.getTripStartDay(),
newTripCities,
member);
}

public void deleteTrip(Long tripIdx) throws NoSuchElementException{
Trip trip = tripRepository.findById(tripIdx)
.orElseThrow(() -> new NoSuchElementException("Trip not found"));

tripRepository.delete(trip);
}

private TripResDto toTripResDto(Trip trip) {
List<TripCity> tripCities = tripCityService.getTripCountry(trip.getTripIdx());
List<City> cities = tripCities.stream().map(tripCity -> tripCity.getCity().toCity()).toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@
import com.hanaro.triptogether.member.domain.Member;
import com.hanaro.triptogether.trip.domain.Trip;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.CreationTimestamp;

import java.time.LocalDateTime;

@Builder
@Entity
@Table(name = "trip_city")
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class TripCity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand All @@ -29,6 +36,7 @@ public class TripCity {
private Member createdBy;

@Column(nullable = false)
@CreationTimestamp
private LocalDateTime createdAt;

private LocalDateTime lastModifiedAt;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.hanaro.triptogether.tripCity.domain;

import com.hanaro.triptogether.trip.domain.Trip;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

Expand All @@ -9,4 +10,5 @@
public interface TripCityRepository extends JpaRepository<TripCity, Long> {

List<TripCity> findAllByTrip_TripIdx(Long tripIdx);
void deleteAllByTrip(Trip trip);
}

0 comments on commit 11cbcd3

Please sign in to comment.