-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from kakao-tech-campus-2nd-step3/feat/2/spot-crud
Spot API 구현 #2
- Loading branch information
Showing
5 changed files
with
214 additions
and
0 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
src/main/java/com/ordertogether/team14_be/spot/controller/SpotController.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,53 @@ | ||
package com.ordertogether.team14_be.spot.controller; | ||
|
||
import com.ordertogether.team14_be.spot.dto.SpotDto; | ||
import com.ordertogether.team14_be.spot.service.SpotService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.List; | ||
|
||
@RestController | ||
public class SpotController { | ||
|
||
private final SpotService spotService; | ||
|
||
@Autowired | ||
public SpotController(SpotService spotService) { | ||
this.spotService = spotService; | ||
} | ||
|
||
//Spot 전체 조회하기 | ||
@GetMapping("/api/v1/spot/{lat}/{lng}") | ||
public ResponseEntity<List<SpotDto>> getSpot(@PathVariable BigDecimal lat, @PathVariable BigDecimal lng) { | ||
return ResponseEntity.ok(spotService.getSpot(lat, lng)); | ||
} | ||
|
||
//Spot 생성하기 | ||
@PostMapping("/api/v1/spot") | ||
public ResponseEntity<SpotDto> createSpot(@RequestBody SpotDto spotDto) { | ||
return ResponseEntity.ok(spotService.createSpot(spotDto)); | ||
} | ||
|
||
//Spot 상세 조회하기 | ||
@GetMapping("/api/v1/spot/{id}") | ||
public ResponseEntity<SpotDto> getSpot(@PathVariable Long id) { | ||
return ResponseEntity.ok(spotService.getSpot(id)); | ||
} | ||
|
||
//Spot 수정하기 | ||
@PutMapping("/api/v1/spot") | ||
public ResponseEntity<SpotDto> updateSpot(@RequestBody SpotDto spotDto) { | ||
return ResponseEntity.ok(spotService.updateSpot(spotDto)); | ||
} | ||
|
||
//Spot 삭제하기 | ||
@DeleteMapping("/api/v1/spot/{id}") | ||
public ResponseEntity<Void> deleteSpot(@PathVariable Long id) { | ||
spotService.deleteSpot(id); | ||
return ResponseEntity.ok().build(); | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/com/ordertogether/team14_be/spot/dto/SpotDto.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,43 @@ | ||
package com.ordertogether.team14_be.spot.dto; | ||
|
||
import com.ordertogether.team14_be.spot.entity.Spot; | ||
import jakarta.persistence.Column; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.math.BigDecimal; | ||
|
||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Getter | ||
public class SpotDto { | ||
private Long id; | ||
@Column(precision = 10, scale = 8) | ||
private BigDecimal lat; | ||
@Column(precision = 11, scale = 8) | ||
private BigDecimal lng; | ||
private String category; | ||
private String store_name; | ||
private int minimum_order_amount; | ||
private String together_order_link; | ||
private String pick_up_location; | ||
private String delivery_status; | ||
|
||
public Spot toEntity() { | ||
return Spot.builder() | ||
.id(id) | ||
.lat(lat) | ||
.lng(lng) | ||
.category(category) | ||
.store_name(store_name) | ||
.minimum_order_amount(minimum_order_amount) | ||
.together_order_link(together_order_link) | ||
.pick_up_location(pick_up_location) | ||
.delivery_status(delivery_status) | ||
.build(); | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/com/ordertogether/team14_be/spot/entity/Spot.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,36 @@ | ||
package com.ordertogether.team14_be.spot.entity; | ||
|
||
import jakarta.persistence.*; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.math.BigDecimal; | ||
|
||
@Entity | ||
@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Getter | ||
public class Spot { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
@Column(precision = 10, scale = 8) | ||
private BigDecimal lat; | ||
|
||
@Column(precision = 11, scale = 8) | ||
private BigDecimal lng; | ||
private String category; | ||
private String store_name; | ||
private Integer minimum_order_amount; | ||
|
||
@Lob | ||
@Column(columnDefinition = "MEDIUMTEXT") | ||
private String together_order_link; | ||
private String pick_up_location; | ||
private String delivery_status; | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/ordertogether/team14_be/spot/repository/SpotRepository.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,13 @@ | ||
package com.ordertogether.team14_be.spot.repository; | ||
|
||
import com.ordertogether.team14_be.spot.entity.Spot; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.List; | ||
|
||
@Repository | ||
public interface SpotRepository extends JpaRepository<Spot, Long> { | ||
List<Spot> findByLatAndLng(BigDecimal lat, BigDecimal lng); | ||
} |
69 changes: 69 additions & 0 deletions
69
src/main/java/com/ordertogether/team14_be/spot/service/SpotService.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,69 @@ | ||
package com.ordertogether.team14_be.spot.service; | ||
|
||
import com.ordertogether.team14_be.spot.dto.SpotDto; | ||
import com.ordertogether.team14_be.spot.entity.Spot; | ||
import com.ordertogether.team14_be.spot.repository.SpotRepository; | ||
import jakarta.persistence.EntityNotFoundException; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
public class SpotService { | ||
|
||
private final SpotRepository spotRepository; | ||
|
||
@Autowired | ||
public SpotService(SpotRepository spotRepository, MemberService memberService) { | ||
this.spotRepository = spotRepository; | ||
} | ||
|
||
//Spot 전체 조회하기 | ||
public List<SpotDto> getSpot(BigDecimal lat, BigDecimal lng) { | ||
return spotRepository.findByLatAndLng(lat, lng) | ||
.stream() | ||
.map(this::toDto) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
public SpotDto createSpot(SpotDto spotDto) { | ||
Spot spot = spotDto.toEntity(); | ||
return toDto(spotRepository.save(spot)); | ||
} | ||
|
||
//Spot 상세 조회하기 | ||
public SpotDto getSpot(Long id) { | ||
Spot spot = spotRepository.findById(id).orElseThrow(() -> new EntityNotFoundException("Spot not found")); | ||
return toDto(spot); | ||
} | ||
|
||
public SpotDto updateSpot(SpotDto spotDto) { | ||
Spot spot = spotRepository.save(spotDto.toEntity()); | ||
return toDto(spot); | ||
} | ||
|
||
public void deleteSpot(Long id) { | ||
spotRepository.deleteById(id); | ||
} | ||
|
||
//Service Layer에서 toDto만들어서 매핑시키기 | ||
public SpotDto toDto(Spot spotInStream) { | ||
Spot spot = spotRepository.findById(spotInStream.getId()).orElseThrow(() -> new EntityNotFoundException("Spot not found")); | ||
|
||
return SpotDto.builder() | ||
.id(spot.getId()) | ||
.lat(spot.getLat()) | ||
.lng(spot.getLng()) | ||
.category(spot.getCategory()) | ||
.store_name(spot.getStore_name()) | ||
.minimum_order_amount(spot.getMinimum_order_amount()) | ||
.together_order_link(spot.getTogether_order_link()) | ||
.pick_up_location(spot.getPick_up_location()) | ||
.delivery_status(spot.getDelivery_status()) | ||
.build(); | ||
|
||
} | ||
} |