Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

Commit

Permalink
feat: guide
Browse files Browse the repository at this point in the history
  • Loading branch information
dongchandev committed Aug 26, 2024
1 parent a89ec20 commit e49283b
Show file tree
Hide file tree
Showing 13 changed files with 309 additions and 4 deletions.
27 changes: 25 additions & 2 deletions src/main/java/com/uiseongcome/controller/GuideInfoController.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,34 @@
package com.uiseongcome.controller;

import com.uiseongcome.dto.GuideDto;
import com.uiseongcome.dto.GuideListRes;
import com.uiseongcome.dto.GuideWithIdRes;
import com.uiseongcome.service.GuideService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/guide")
public class GuideInfoController {
private final GuideService guideService;

@PostMapping
public void write(@RequestBody GuideDto guideDto){
guideService.append(guideDto);
}

@GetMapping("/list")
public List<GuideListRes> getList(){
return guideService.getList();
}

@GetMapping("/{id}")
public GuideWithIdRes get(@PathVariable Long id){
return guideService.get(id);
}
}
18 changes: 17 additions & 1 deletion src/main/java/com/uiseongcome/domain/GuideInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,23 @@ public class GuideInfo {

private Boolean parking;

private Long bedCnt;

private Long bathroomCnt;

private Long roomCnt;

private String address;

@Enumerated(EnumType.STRING)
private GuideKind guideKind;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "fk_user_id")
private User user;

@Builder
public GuideInfo(Long guideInfoId, String title, LocalDate startAt, LocalDate endAt, Long limitPeople, Long price, String description, Boolean wifi, Boolean tv, Boolean kitchen, Boolean washingMachine, Boolean airConditioner, Boolean parking, User user) {
public GuideInfo(Long guideInfoId, String title, LocalDate startAt, LocalDate endAt, Long limitPeople, Long price, String description, Boolean wifi, Boolean tv, Boolean kitchen, Boolean washingMachine, Boolean airConditioner, Boolean parking, Long bedCnt, Long bathroomCnt, Long roomCnt, String address, GuideKind guideKind, User user) {
this.guideInfoId = guideInfoId;
this.title = title;
this.startAt = startAt;
Expand All @@ -60,6 +71,11 @@ public GuideInfo(Long guideInfoId, String title, LocalDate startAt, LocalDate en
this.washingMachine = washingMachine;
this.airConditioner = airConditioner;
this.parking = parking;
this.bedCnt = bedCnt;
this.bathroomCnt = bathroomCnt;
this.roomCnt = roomCnt;
this.address = address;
this.guideKind = guideKind;
this.user = user;
}
}
8 changes: 8 additions & 0 deletions src/main/java/com/uiseongcome/domain/GuideKind.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.uiseongcome.domain;

public enum GuideKind {
NATURE,
FOOD,
REST,
EXPERIENCE
}
8 changes: 7 additions & 1 deletion src/main/java/com/uiseongcome/domain/Place.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,21 @@ public class Place {
//경도
private String longitude;

private String name;

private String seq;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "fk_guide_info_id")
private GuideInfo guideInfo;

@Builder
public Place(Long placeId, String latitude, String longitude, GuideInfo guideInfo) {
public Place(Long placeId, String latitude, String longitude, String name, String seq, GuideInfo guideInfo) {
this.placeId = placeId;
this.latitude = latitude;
this.longitude = longitude;
this.name = name;
this.seq = seq;
this.guideInfo = guideInfo;
}
}
16 changes: 16 additions & 0 deletions src/main/java/com/uiseongcome/dto/FileDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.uiseongcome.dto;

import com.uiseongcome.domain.File;
import com.uiseongcome.domain.GuideInfo;

public record FileDto(
String path
) {
public File toEntity(GuideInfo guideInfo){
return File.builder()
.path(path)
.guideInfo(guideInfo)
.build();
}

}
55 changes: 55 additions & 0 deletions src/main/java/com/uiseongcome/dto/GuideDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.uiseongcome.dto;

import com.uiseongcome.domain.File;
import com.uiseongcome.domain.GuideInfo;
import com.uiseongcome.domain.GuideKind;
import com.uiseongcome.domain.User;

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

public record GuideDto(
String title,
LocalDate startAt,
LocalDate endAt,
Long limitPeople,
Long price,
String description,
Boolean wifi,
Boolean tv,
Boolean kitchen,
Boolean washingMachine,
Boolean airConditioner,
Boolean parking,
Long bedCnt,
Long bathroomCnt,
Long roomCnt,
String address,
GuideKind guideKind,
List<PlaceDto> places,
List<FileDto> files,
String userId
) {
public GuideInfo toEntity(User user){
return GuideInfo.builder()
.title(title)
.startAt(startAt)
.endAt(endAt)
.limitPeople(limitPeople)
.price(price)
.description(description)
.wifi(wifi)
.tv(tv)
.kitchen(kitchen)
.washingMachine(washingMachine)
.airConditioner(airConditioner)
.parking(parking)
.bedCnt(bedCnt)
.bathroomCnt(bathroomCnt)
.roomCnt(roomCnt)
.address(address)
.guideKind(guideKind)
.user(user)
.build();
}
}
21 changes: 21 additions & 0 deletions src/main/java/com/uiseongcome/dto/GuideListRes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.uiseongcome.dto;

import com.uiseongcome.domain.GuideInfo;
import com.uiseongcome.domain.Place;

import java.util.List;

public record GuideListRes(
String title,
List<String> places,
Long price,
String name
) {
public static GuideListRes of(GuideInfo guideInfo, List<Place> places){
return new GuideListRes(
guideInfo.getTitle(),
places.stream().map(Place::getName).toList(),
guideInfo.getPrice(),
guideInfo.getUser().getName());
}
}
58 changes: 58 additions & 0 deletions src/main/java/com/uiseongcome/dto/GuideWithIdRes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.uiseongcome.dto;

import com.uiseongcome.domain.*;

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

public record GuideWithIdRes(
Long guideId,
String title,
LocalDate startAt,
LocalDate endAt,
Long limitPeople,
Long price,
String description,
Boolean wifi,
Boolean tv,
Boolean kitchen,
Boolean washingMachine,
Boolean airConditioner,
Boolean parking,
Long bedCnt,
Long bathroomCnt,
Long roomCnt,
String address,
GuideKind guideKind,
List<PlaceDto> places,
List<String> files,
String userId,
String userName
) {
public static GuideWithIdRes of(GuideInfo guideInfo, List<Place> places, List<File> files){
return new GuideWithIdRes(
guideInfo.getGuideInfoId(),
guideInfo.getTitle(),
guideInfo.getStartAt(),
guideInfo.getEndAt(),
guideInfo.getLimitPeople(),
guideInfo.getPrice(),
guideInfo.getDescription(),
guideInfo.getWifi(),
guideInfo.getTv(),
guideInfo.getKitchen(),
guideInfo.getWashingMachine(),
guideInfo.getAirConditioner(),
guideInfo.getParking(),
guideInfo.getBedCnt(),
guideInfo.getBathroomCnt(),
guideInfo.getRoomCnt(),
guideInfo.getAddress(),
guideInfo.getGuideKind(),
places.stream().map(PlaceDto::of).toList(),
files.stream().map(File::getPath).toList(),
guideInfo.getUser().getUserId(),
guideInfo.getUser().getName()
);
}
}
28 changes: 28 additions & 0 deletions src/main/java/com/uiseongcome/dto/PlaceDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.uiseongcome.dto;

import com.uiseongcome.domain.Place;

public record PlaceDto(
String latitude,
String longitude,
String name,
String seq
) {
public Place toEntity(){
return Place.builder()
.latitude(latitude)
.longitude(longitude)
.name(name)
.seq(seq)
.build();
}

public static PlaceDto of(Place place){
return new PlaceDto(
place.getLatitude(),
place.getLongitude(),
place.getName(),
place.getSeq()
);
}
}
11 changes: 11 additions & 0 deletions src/main/java/com/uiseongcome/repository/FileRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.uiseongcome.repository;

import com.uiseongcome.domain.File;
import com.uiseongcome.domain.GuideInfo;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface FileRepository extends JpaRepository<File, Long> {
List<File> findByGuideInfo(GuideInfo guideInfo);
}
11 changes: 11 additions & 0 deletions src/main/java/com/uiseongcome/repository/PlaceRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.uiseongcome.repository;

import com.uiseongcome.domain.GuideInfo;
import com.uiseongcome.domain.Place;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface PlaceRepository extends JpaRepository<Place, Long> {
List<Place> findByGuideInfo(GuideInfo guideInfo);
}
8 changes: 8 additions & 0 deletions src/main/java/com/uiseongcome/repository/UserRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.uiseongcome.repository;

import com.uiseongcome.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository<User, String> {
User findById();
}
44 changes: 44 additions & 0 deletions src/main/java/com/uiseongcome/service/GuideService.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,56 @@
package com.uiseongcome.service;

import com.uiseongcome.domain.File;
import com.uiseongcome.domain.GuideInfo;
import com.uiseongcome.domain.Place;
import com.uiseongcome.domain.User;
import com.uiseongcome.dto.GuideDto;
import com.uiseongcome.dto.GuideListRes;
import com.uiseongcome.dto.GuideWithIdRes;
import com.uiseongcome.dto.PlaceDto;
import com.uiseongcome.repository.FileRepository;
import com.uiseongcome.repository.GuideInfoRepository;
import com.uiseongcome.repository.PlaceRepository;
import com.uiseongcome.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class GuideService {
private final GuideInfoRepository guideInfoRepository;
private final UserRepository userRepository;
private final PlaceRepository placeRepository;
private final FileRepository fileRepository;

@Transactional(rollbackFor = Exception.class)
public void append(GuideDto guideDto){
User user = userRepository.findById(guideDto.userId()).get();
GuideInfo guideInfo = guideInfoRepository.save(guideDto.toEntity(user));
placeRepository.saveAll(guideDto.places().stream()
.map(PlaceDto::toEntity)
.toList());
fileRepository.saveAll(guideDto.files().stream().map(f -> f.toEntity(guideInfo)).toList());
}

@Transactional(readOnly = true)
public List<GuideListRes> getList(){
List<GuideInfo> guideInfos = guideInfoRepository.findAll();
return guideInfos.stream()
.map(g -> GuideListRes.of(
g, placeRepository.findByGuideInfo(g)))
.toList();

}

@Transactional(readOnly = true)
public GuideWithIdRes get(Long id){
GuideInfo guideInfo = guideInfoRepository.findById(id).get();
List<Place> places = placeRepository.findByGuideInfo(guideInfo);
List<File> files = fileRepository.findByGuideInfo(guideInfo);
return GuideWithIdRes.of(guideInfo, places, files);
}
}

0 comments on commit e49283b

Please sign in to comment.