diff --git a/src/main/java/com/ordertogether/team14_be/spot/controller/SpotController.java b/src/main/java/com/ordertogether/team14_be/spot/controller/SpotController.java new file mode 100644 index 00000000..902a8fa6 --- /dev/null +++ b/src/main/java/com/ordertogether/team14_be/spot/controller/SpotController.java @@ -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> getSpot(@PathVariable BigDecimal lat, @PathVariable BigDecimal lng) { + return ResponseEntity.ok(spotService.getSpot(lat, lng)); + } + + //Spot 생성하기 + @PostMapping("/api/v1/spot") + public ResponseEntity createSpot(@RequestBody SpotDto spotDto) { + return ResponseEntity.ok(spotService.createSpot(spotDto)); + } + + //Spot 상세 조회하기 + @GetMapping("/api/v1/spot/{id}") + public ResponseEntity getSpot(@PathVariable Long id) { + return ResponseEntity.ok(spotService.getSpot(id)); + } + + //Spot 수정하기 + @PutMapping("/api/v1/spot") + public ResponseEntity updateSpot(@RequestBody SpotDto spotDto) { + return ResponseEntity.ok(spotService.updateSpot(spotDto)); + } + + //Spot 삭제하기 + @DeleteMapping("/api/v1/spot/{id}") + public ResponseEntity deleteSpot(@PathVariable Long id) { + spotService.deleteSpot(id); + return ResponseEntity.ok().build(); + } + +} diff --git a/src/main/java/com/ordertogether/team14_be/spot/dto/SpotDto.java b/src/main/java/com/ordertogether/team14_be/spot/dto/SpotDto.java new file mode 100644 index 00000000..61f6047a --- /dev/null +++ b/src/main/java/com/ordertogether/team14_be/spot/dto/SpotDto.java @@ -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(); + } + +} diff --git a/src/main/java/com/ordertogether/team14_be/spot/entity/Spot.java b/src/main/java/com/ordertogether/team14_be/spot/entity/Spot.java new file mode 100644 index 00000000..edbcf92f --- /dev/null +++ b/src/main/java/com/ordertogether/team14_be/spot/entity/Spot.java @@ -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; + +} diff --git a/src/main/java/com/ordertogether/team14_be/spot/repository/SpotRepository.java b/src/main/java/com/ordertogether/team14_be/spot/repository/SpotRepository.java new file mode 100644 index 00000000..13649a86 --- /dev/null +++ b/src/main/java/com/ordertogether/team14_be/spot/repository/SpotRepository.java @@ -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 { + List findByLatAndLng(BigDecimal lat, BigDecimal lng); +} diff --git a/src/main/java/com/ordertogether/team14_be/spot/service/SpotService.java b/src/main/java/com/ordertogether/team14_be/spot/service/SpotService.java new file mode 100644 index 00000000..6ebaa300 --- /dev/null +++ b/src/main/java/com/ordertogether/team14_be/spot/service/SpotService.java @@ -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 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(); + + } +}