Skip to content

Commit

Permalink
Merge pull request #8 from kakao-tech-campus-2nd-step3/feat/2/spot-crud
Browse files Browse the repository at this point in the history
Spot API 구현 #2
  • Loading branch information
rbm0524 authored Sep 20, 2024
2 parents 0fb2f33 + 0ea24e4 commit b08604b
Show file tree
Hide file tree
Showing 5 changed files with 214 additions and 0 deletions.
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 src/main/java/com/ordertogether/team14_be/spot/dto/SpotDto.java
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 src/main/java/com/ordertogether/team14_be/spot/entity/Spot.java
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;

}
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);
}
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();

}
}

0 comments on commit b08604b

Please sign in to comment.