Skip to content

Commit

Permalink
Merge branch 'develop33'
Browse files Browse the repository at this point in the history
  • Loading branch information
shinheekim committed Jun 17, 2024
2 parents 81512b4 + e317742 commit d3255ca
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.skhu.tastyinventory_be.controller.sold;

import com.fasterxml.jackson.databind.ser.Serializers;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -9,7 +8,6 @@
import net.skhu.tastyinventory_be.controller.sold.dto.response.SoldResponseDto;
import net.skhu.tastyinventory_be.exception.SuccessCode;
import net.skhu.tastyinventory_be.service.SoldService;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;
Expand All @@ -21,26 +19,28 @@
public class SoldController {
private final SoldService soldService;

@PostMapping
/* @PostMapping
@ResponseStatus(HttpStatus.CREATED)
public BaseResponse<?> registerSold(@RequestBody @Valid SoldRequestDto requestDto) {
soldService.registerSold(requestDto);
return BaseResponse.success(SuccessCode.SOLD_CREATE_SUCCESS);
}
}*/

@GetMapping
public BaseResponse<List<SoldResponseDto>> findAllSold() {
List<SoldResponseDto> soldResponseDtoList = soldService.findAllSold();
return BaseResponse.success(SuccessCode.GET_SUCCESS, soldResponseDtoList);
}
@PatchMapping("/{id}")

@PutMapping("/{id}")
public BaseResponse<?> updateSold(@PathVariable("id") Long id, @RequestBody @Valid SoldRequestDto requestDto) {
soldService.updateSold(id, requestDto);
return BaseResponse.success(SuccessCode.SOLD_PATCH_SUCCESSCODE);
}

@DeleteMapping("/{id}")
/* @DeleteMapping("/{id}")
public BaseResponse<?> deleteSold(@PathVariable("id") Long id) {
soldService.deleteSold(id);
return BaseResponse.success(SuccessCode.SOLD_DELETE_SUCCESSCODE);
}
}*/
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.skhu.tastyinventory_be.controller.sold.dto.request;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import lombok.AllArgsConstructor;
import lombok.Getter;
Expand All @@ -19,5 +20,6 @@ public class SoldRequestDto {
)
private String date;

@NotNull
private List<SoldMenuDto> soldMenuList;
}
20 changes: 12 additions & 8 deletions src/main/java/net/skhu/tastyinventory_be/domain/sold/Sold.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import net.skhu.tastyinventory_be.controller.sold.dto.request.SoldMenuDto;
import net.skhu.tastyinventory_be.controller.sold.dto.request.SoldRequestDto;

import net.skhu.tastyinventory_be.domain.BaseEntity;
import net.skhu.tastyinventory_be.domain.menu.Menu;
import org.springframework.cglib.core.Local;

import java.time.LocalDate;
import java.util.List;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "SOLD")
@Entity
public class Sold extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(nullable = false)
private LocalDate date;

Expand All @@ -30,13 +31,16 @@ public class Sold extends BaseEntity {
private Long count;

@Builder
public Sold(Long id, LocalDate date, Menu menu, Long count) {
this.setId(id);
public Sold(LocalDate date, Menu menu, Long count) {
this.date = date;
this.menu = menu;
this.count = count;
}
public void update(Long count) {
this.count = count; // ํŒ๋งค๋Ÿ‰์„ ์—…๋ฐ์ดํŠธ
public void update(Long newCount) {
this.count = newCount; // ํŒ๋งค๋Ÿ‰์„ ์—…๋ฐ์ดํŠธ
}

public void setCount(long count) {
this.count = count;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@
import java.util.Optional;

public interface SoldRepository extends JpaRepository<Sold, Long> {
List<Sold> findByDate(LocalDate date);
Optional<Sold> findByDateAndMenu(LocalDate date, Menu menu);
List<Sold> findByMenuId(Long id);
}
71 changes: 34 additions & 37 deletions src/main/java/net/skhu/tastyinventory_be/service/SoldService.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,79 +10,76 @@
import net.skhu.tastyinventory_be.domain.sold.SoldRepository;
import net.skhu.tastyinventory_be.exception.ErrorCode;
import net.skhu.tastyinventory_be.exception.model.NotFoundException;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@RequiredArgsConstructor
@Transactional(readOnly = true)
@Service
public class SoldService {
private final SoldRepository soldRepository;
private final MenuRepository menuRepository;

private static final Logger logger = LoggerFactory.getLogger(SoldService.class);

@Transactional
public void registerSold(SoldRequestDto requestDto) {
for (int i = 0; i < requestDto.getSoldMenuList().size(); i++) {
Long menuId = requestDto.getSoldMenuList().get(i).getMenuId();
Long count = requestDto.getSoldMenuList().get(i).getSoldCount();

Menu menu = menuRepository.findById(menuId).orElseThrow(
() -> new NotFoundException(
ErrorCode.NOT_FOUND_MENU_EXCEPTION,
ErrorCode.NOT_FOUND_MENU_EXCEPTION.getMessage()
));

Sold sold = Sold.builder()
.date(LocalDate.parse(requestDto.getDate()))
.menu(menu)
.count(count)
.build();

soldRepository.save(sold);
}
}

public List<SoldResponseDto> findAllSold() {
List<Sold> soldList = soldRepository.findAll();
return soldList.stream()
.map(sold -> new SoldResponseDto (sold.getId(), sold.getMenu().getName(), sold.getCount()))
List<Menu> menuList = menuRepository.findAll();
return menuList.stream()
.map(menu -> {
Sold sold = soldRepository.findByMenuId(menu.getId())
.stream()
.findFirst() // ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ ์ฒซ ๋ฒˆ์งธ ํŒ๋งค ์—”ํ‹ฐํ‹ฐ ๊ฐ€์ ธ์˜ค๊ธฐ
.orElse(null);
Long soldCount = sold != null ? sold.getCount() : 0L;
return new SoldResponseDto(menu.getId(), menu.getName(), soldCount);
})
.collect(Collectors.toList());
}

@Transactional
public void updateSold(Long id, SoldRequestDto requestDto) {
Sold sold = soldRepository.findById(id).orElseThrow(
() -> new NotFoundException(
ErrorCode.NOT_FOUND_SOLD_EXCEPTION,
ErrorCode.NOT_FOUND_SOLD_EXCEPTION.getMessage() + id));

for (SoldMenuDto soldMenuDto : requestDto.getSoldMenuList()) {
Menu menu = menuRepository.findById(soldMenuDto.getMenuId()).orElseThrow(
() -> new NotFoundException(
ErrorCode.NOT_FOUND_MENU_EXCEPTION,
ErrorCode.NOT_FOUND_MENU_EXCEPTION.getMessage() + id));

sold.update(soldMenuDto.getSoldCount());
List<Sold> soldList = soldRepository.findByMenuId(menu.getId());
Sold sold;
if (soldList.isEmpty()) {
sold = Sold.builder()
.date(LocalDate.parse(requestDto.getDate()))
.menu(menu)
.count(soldMenuDto.getSoldCount())
.build();
} else {
sold = soldList.get(0);
sold.update(soldMenuDto.getSoldCount());
}
soldRepository.save(sold);
}
}

/*
@Transactional
public void deleteSold(Long id) {
Sold sold = soldRepository.findById(id).orElseThrow(
() -> new NotFoundException(
ErrorCode.NOT_FOUND_SOLD_EXCEPTION,
ErrorCode.NOT_FOUND_SOLD_EXCEPTION.getMessage() + id));
soldRepository.delete(sold);
}*/
@Scheduled(cron = "0 0 0 * * *") // ๋งค์ผ ์ž์ •์— ์‹คํ–‰
@Transactional
public void resetSoldCounts() {
// Sold ์—”ํ‹ฐํ‹ฐ์˜ ๋ชจ๋“  ํŒ๋งค๋Ÿ‰(count)์„ 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•ฉ๋‹ˆ๋‹ค.
List<Sold> soldList = soldRepository.findAll();
for (Sold sold : soldList) {
sold.setCount(0L);
}
soldRepository.saveAll(soldList);
}

}

0 comments on commit d3255ca

Please sign in to comment.