Skip to content

Commit

Permalink
location 연결 및 location별 조회 추가#3
Browse files Browse the repository at this point in the history
  • Loading branch information
firefox1234123 committed Jul 29, 2024
1 parent 9f2a2ab commit 521ca87
Show file tree
Hide file tree
Showing 15 changed files with 252 additions and 21 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ dependencies {
runtimeOnly 'com.mysql:mysql-connector-j'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
// testImplementation 'org.springframework.security:spring-security-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'

//aws
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package net.skhu.likelion12thteam03be.location.api.dto;

import lombok.RequiredArgsConstructor;
import net.skhu.likelion12thteam03be.category.api.dto.response.CategoryListResDto;
import net.skhu.likelion12thteam03be.location.api.dto.request.LocationSaveReqDto;
import net.skhu.likelion12thteam03be.location.api.dto.request.LocationUpdateReqDto;
import net.skhu.likelion12thteam03be.location.api.dto.response.LocationListResDto;
import net.skhu.likelion12thteam03be.location.application.LocationService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/locations")
public class LocationController {
private final LocationService locationService;

@PostMapping()
public ResponseEntity<String> locationSave(@RequestBody LocationSaveReqDto locationSaveReqDto) {
locationService.locationSave(locationSaveReqDto);
return new ResponseEntity<>("Successful Location Save!", HttpStatus.CREATED);
}

@GetMapping()
public ResponseEntity<LocationListResDto> locationFindAll() {
LocationListResDto locationListResDto = locationService.locationFindAll();
return new ResponseEntity<>(locationListResDto, HttpStatus.OK);
}

@PatchMapping("/{locationId}")
public ResponseEntity<String> locationUpdate(@PathVariable Long locationId, @RequestBody LocationUpdateReqDto locationUpdateReqDto) {
locationService.locationUpdate(locationId, locationUpdateReqDto);
return new ResponseEntity<>("Successful Location Update! locationId = " + locationId, HttpStatus.OK);
}

@DeleteMapping("/{locationId}")
public ResponseEntity<String> locationDelete(@PathVariable Long locationId) {
locationService.locationDelete(locationId);
return new ResponseEntity<>("Successful Location Delete! locationId = " + locationId, HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package net.skhu.likelion12thteam03be.location.api.dto.request;

public record LocationSaveReqDto(
String name
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package net.skhu.likelion12thteam03be.location.api.dto.request;

public record LocationUpdateReqDto(
String name
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package net.skhu.likelion12thteam03be.location.api.dto.response;

import lombok.Builder;
import net.skhu.likelion12thteam03be.location.domain.Location;

@Builder
public record LocationInfoResDto(
Long locationId,
String name
) {
public static LocationInfoResDto from(Location location) {
return LocationInfoResDto.builder()
.name(location.getName())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package net.skhu.likelion12thteam03be.location.api.dto.response;

import lombok.Builder;

import java.util.List;

@Builder
public record LocationListResDto(
List<LocationInfoResDto> locaitonList
) {
public static LocationListResDto from(List<LocationInfoResDto> locationList) {
return LocationListResDto.builder()
.locaitonList(locationList)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package net.skhu.likelion12thteam03be.location.application;

import lombok.RequiredArgsConstructor;
import net.skhu.likelion12thteam03be.location.api.dto.request.LocationSaveReqDto;
import net.skhu.likelion12thteam03be.location.api.dto.request.LocationUpdateReqDto;
import net.skhu.likelion12thteam03be.location.api.dto.response.LocationInfoResDto;
import net.skhu.likelion12thteam03be.location.api.dto.response.LocationListResDto;
import net.skhu.likelion12thteam03be.location.domain.Location;
import net.skhu.likelion12thteam03be.location.domain.repository.LocationRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
@RequiredArgsConstructor
public class LocationService {
private final LocationRepository locationRepository;

@Transactional
public void locationSave(LocationSaveReqDto locationSaveReqDto) {
if (locationRepository.existsByName(locationSaveReqDto.name())) {
throw new IllegalArgumentException("해당 위치가 이미 존재합니다.");
}
Location location = Location.builder()
.name(locationSaveReqDto.name())
.build();

locationRepository.save(location);
}

public LocationListResDto locationFindAll() {
List<Location> locations = locationRepository.findAll();

List<LocationInfoResDto> locationInfoResDtoList = locations.stream()
.map(LocationInfoResDto::from)
.toList();

return LocationListResDto.from(locationInfoResDtoList);
}

@Transactional
public void locationUpdate(Long locationId, LocationUpdateReqDto locationUpdateReqDto) {
Location location = locationRepository.findById(locationId).orElseThrow(
() -> new IllegalArgumentException("해당 위치가 존재하지 않습니다. 새 위치로 등록해주세요.")
);
location.update(locationUpdateReqDto.name());
}

@Transactional
public void locationDelete(Long locationId) {
Location location = locationRepository.findById(locationId).orElseThrow(
() -> new IllegalArgumentException("해당 위치가 존재하지 않습니다.")
);
locationRepository.delete(location);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package net.skhu.likelion12thteam03be.location.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import net.skhu.likelion12thteam03be.post.domain.Post;

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

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Location {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "location_id")
private Long locationId;

private String name;

@JsonIgnore
@OneToMany(mappedBy = "location", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Post> postList = new ArrayList<>();

@Builder
public Location(String name) {
this.name = name;
}

public void update(String name) {
this.name = name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package net.skhu.likelion12thteam03be.location.domain.repository;

import net.skhu.likelion12thteam03be.location.domain.Location;
import org.springframework.data.jpa.repository.JpaRepository;

public interface LocationRepository extends JpaRepository<Location, Long> {
boolean existsByName(String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,24 @@ public ResponseEntity<PostInfoResDto> postFindById(@PathVariable Long postId) {
return new ResponseEntity<>(postInfoResDto, HttpStatus.OK);
}

// 글 위치별 조회
@GetMapping("/locations/{locationId}")
public ResponseEntity<PostListResDto> postFindByLocationId(@PathVariable("locationId") Long locationId) {
PostListResDto postListResDto = postService.postFindByLocationId(locationId);
return new ResponseEntity<>(postListResDto, HttpStatus.OK);
}

// 글 카테고리별 조회
@GetMapping("/categories/{categoryId}")
public ResponseEntity<PostListResDto> postFindByCategoryId(@PathVariable("categoryId") Long categoryId) {
PostListResDto postListResDto = postService.postFindByCategoryId(categoryId);
return new ResponseEntity<>(postListResDto, HttpStatus.OK);
}

// 글 감정별 조회

// 글 색상별 조회

// 글 작성자별 조회(내 글 조회)
/*@GetMapping("/users/{userId}")
public ResponseEntity<PostListResDto> postFindByUserId(@PathVariable("userId") Long userId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ public record PostSaveReqDto(
String title,
String content,
String imgUrl,
String locationId,
Long locationId,
Integer time,
Integer price,
Long categoryId,
String emotionId,
String colorId
Long emotionId,
Long colorId
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ public record PostUpdateReqDto(
String title,
String content,
String imgUrl,
String locationId,
Long locationId,
Integer time,
Integer price,
Long categoryId,
String emotionId,
String colorId
Long emotionId,
Long colorId
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,27 @@

import lombok.Builder;
import net.skhu.likelion12thteam03be.category.domain.Category;
import net.skhu.likelion12thteam03be.location.domain.Location;
import net.skhu.likelion12thteam03be.post.domain.Post;

@Builder
public record PostInfoResDto(
String title,
String content,
String imgUrl,
String locationId,
Location location,
Integer time,
Integer price,
Category category,
String emotionId,
String colorId
Long emotionId,
Long colorId
) {
public static PostInfoResDto from(Post post) {
return PostInfoResDto.builder()
.title(post.getTitle())
.content(post.getContent())
.imgUrl(post.getImgUrl())
.locationId(post.getLocationId())
.location(post.getLocation())
.time(post.getTime())
.price(post.getPrice())
.category(post.getCategory())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import lombok.RequiredArgsConstructor;
import net.skhu.likelion12thteam03be.category.domain.Category;
import net.skhu.likelion12thteam03be.category.domain.repository.CategoryRepository;
import net.skhu.likelion12thteam03be.location.domain.Location;
import net.skhu.likelion12thteam03be.location.domain.repository.LocationRepository;
import net.skhu.likelion12thteam03be.post.api.dto.request.PostSaveReqDto;
import net.skhu.likelion12thteam03be.post.api.dto.request.PostUpdateReqDto;
import net.skhu.likelion12thteam03be.post.api.dto.response.PostInfoResDto;
Expand All @@ -21,17 +23,21 @@
public class PostService {
private final PostRepository postRepository;
private final CategoryRepository categoryRepository;
private final LocationRepository locationRepository;

@Transactional
public void postSave(PostSaveReqDto postSaveReqDto) {
Category category = categoryRepository.findById(postSaveReqDto.categoryId())
.orElseThrow(() -> new IllegalArgumentException("해당 카테고리가 존재하지 않습니다. categoryId = " + postSaveReqDto.categoryId()));

Location location = locationRepository.findById(postSaveReqDto.locationId())
.orElseThrow(() -> new IllegalArgumentException("해당 위치가 존재하지 않습니다. locationId = " + postSaveReqDto.locationId()));

Post post = Post.builder()
.title(postSaveReqDto.title())
.content(postSaveReqDto.content())
.imgUrl(postSaveReqDto.imgUrl())
.locationId(postSaveReqDto.locationId())
.location(location)
.time(postSaveReqDto.time())
.price(postSaveReqDto.price())
.category(category)
Expand Down Expand Up @@ -61,6 +67,17 @@ public PostInfoResDto postFindById(Long postId) {
return PostInfoResDto.from(post);
}

// 글 위치별 조회
public PostListResDto postFindByLocationId(Long locationId) {
Optional<Post> post = postRepository.findById(locationId);

List<PostInfoResDto> postInfoResDtoList = post.stream()
.map(PostInfoResDto::from)
.toList();

return PostListResDto.from(postInfoResDtoList);
}

// 글 카테고리별 조회
public PostListResDto postFindByCategoryId(Long categoryId) {
Optional<Post> posts = postRepository.findById(categoryId);
Expand All @@ -83,6 +100,9 @@ public PostListResDto postFindByUserId(Long userId) {
return PostListResDto.from(postInfoResDtoList);
}

// 글 감정별 조회

// 글 색상별 조회

// 글 검색 조회

Expand All @@ -97,7 +117,10 @@ public void postUpdate(Long postId, PostUpdateReqDto postUpdateReqDto) {
Category category = categoryRepository.findById(postUpdateReqDto.categoryId())
.orElseThrow(() -> new IllegalArgumentException("해당 카테고리가 존재하지 않습니다. categoryId = " + postUpdateReqDto.categoryId()));

post.update(category, postUpdateReqDto);
Location location = locationRepository.findById(postUpdateReqDto.locationId())
.orElseThrow(() -> new IllegalArgumentException("해당 위치가 존재하지 않습니다. locationId = " + postUpdateReqDto.locationId()));

post.update(location, category, postUpdateReqDto);
PostInfoResDto.from(post);
}

Expand Down
Loading

0 comments on commit 521ca87

Please sign in to comment.