본문 바로가기

프로젝트_ 커피주문 서비스

Store CRUD

  • Store 테이블 추가

  • Entity/Store
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Getter
@Entity
public class Store {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column
    private String name;

    @Column
    private String location;

    public void patch(Store store) {
        if (store.name != null) {
            this.name = store.name;
        }
        if (store.location != null) {
            this.location = store.location;
        }
    }
}

 

  • StoreRepository
public interface StoreRepository extends JpaRepository<Store, Long> {

    //지역별 지점 조회
    @Query(value = "SELECT * FROM store WHERE location = :location", nativeQuery = true)
    List<Store> findByLocation(@Param("location") String location);
}

 

  • StoreDto
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Setter
public class StoreDto {
    private Long id;
    private String name;
    private String location;

    public Store toEntity() {
        return new Store(id, name, location);
    }
}

 

  • StoreService
@Slf4j
@Service
public class StoreService {

    @Autowired
    private StoreRepository storeRepository;

    //전제 지점 조회
    public List<Store> list() {
        return storeRepository.findAll();
    }

    //지역 별 지점 조회
    public List<Store> listByLocation(String location) {
        return storeRepository.findByLocation(location);
    }

    //지점 등록
    @Transactional
    public Store create(StoreDto dto) {
        Store store = dto.toEntity();
        if (store.getId() != null) {
            return null;
        }
        return storeRepository.save(store);
    }

    //지점 수정
    @Transactional
    public Store update(Long id, StoreDto dto) {
        Store store = dto.toEntity();
        Store target = storeRepository.findById(id).orElse(null);

        target.patch(store);
        Store updated = storeRepository.save(target);
        return updated;
    }

    //지점 삭제
    @Transactional
    public Store delete(Long id) {
        Store target = storeRepository.findById(id).orElse(null);

        if (target == null) {
            return null;
        }
        storeRepository.delete(target);

        return target;
    }
}

 

  • StoreAPIController
@Slf4j
@RestController
public class StoreAPIController {

    @Autowired
    private StoreService storeService;

    //전체 지점 조회
    @GetMapping("/api/stores")
    public List<Store> list() {
        return storeService.list();
    }

    //지역 별 지점 조회
    @GetMapping("/api/stores/{location}")
    public List<Store> listByLocation(@PathVariable String location) {
        return storeService.listByLocation(location);
    }

    //지점 등록
    @PostMapping("/api/store")
    public ResponseEntity<Store> create(@RequestBody StoreDto dto) {
        Store created = storeService.create(dto);
        return (created != null) ?
                ResponseEntity.status(HttpStatus.OK).body(created) :
                ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
    }

    //지점 수정
    @PatchMapping("/api/store/{id}")
    public ResponseEntity<Store> update(@PathVariable Long id, @RequestBody StoreDto dto) {
        Store updated = storeService.update(id, dto);
        return (updated != null) ?
                ResponseEntity.status(HttpStatus.OK).body(updated) :
                ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
    }

    //지점 삭제
    @DeleteMapping("/api/store/{id}")
    public ResponseEntity<Store> delete(@PathVariable Long id) {
        Store deleted = storeService.delete(id);
        return (deleted != null) ?
                ResponseEntity.status(HttpStatus.OK).build() :
                ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
    }

}

 

  • 에버랜드점(용인) 추가

 

 

  • 전체 지점 조회

 

 

  • 용인 지역의 지점 조회

 

 

  • 강남점(서울) -> 부산점(부산)으로 수정

 

 

  • 부산점 삭제

 

 

 

GitHub - Pearlmoon997/CoffeeShop: CoffeeShop

CoffeeShop. Contribute to Pearlmoon997/CoffeeShop development by creating an account on GitHub.

github.com

 

'프로젝트_ 커피주문 서비스' 카테고리의 다른 글

레이아웃, Member 변경  (0) 2022.08.11
Store - Order 연결  (0) 2022.08.09
Order 삭제  (0) 2022.08.07
Order 조회, Order_Product 등록, 삭제  (0) 2022.08.05
OrderProduct_ SELECT 테스트  (0) 2022.07.30