-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9f2a2ab
commit 521ca87
Showing
15 changed files
with
252 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/main/java/net/skhu/likelion12thteam03be/location/api/dto/LocationController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/net/skhu/likelion12thteam03be/location/api/dto/request/LocationSaveReqDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) { | ||
} |
6 changes: 6 additions & 0 deletions
6
...ain/java/net/skhu/likelion12thteam03be/location/api/dto/request/LocationUpdateReqDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) { | ||
} |
16 changes: 16 additions & 0 deletions
16
...main/java/net/skhu/likelion12thteam03be/location/api/dto/response/LocationInfoResDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...main/java/net/skhu/likelion12thteam03be/location/api/dto/response/LocationListResDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/main/java/net/skhu/likelion12thteam03be/location/application/LocationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/net/skhu/likelion12thteam03be/location/domain/Location.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...ain/java/net/skhu/likelion12thteam03be/location/domain/repository/LocationRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.