Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
5.0 kB
1
Indexable
Never
@RestController
@RequestMapping("/deals")
public class DealController {
    private final DealService dealService;

    @Autowired
    public DealController(DealService dealService) {
        this.dealService = dealService;
    }

    @PostMapping
    public ResponseEntity<Deal> createDeal(@RequestBody Deal deal) {
        Deal createdDeal = dealService.createDeal(deal);
        return ResponseEntity.ok(createdDeal);
    }

    @PostMapping("/{dealId}/buy")
    public ResponseEntity<String> buyDeal(@PathVariable int dealId, @RequestParam int userId) {
        boolean success = dealService.buyDeal(dealId, userId);
        if (success) {
            return ResponseEntity.ok("Deal purchased successfully");
        } else {
            return ResponseEntity.badRequest().body("Failed to purchase deal");
        }
    }

    @PutMapping("/{dealId}")
    public ResponseEntity<String> updateDeal(@PathVariable int dealId, @RequestBody Deal updatedDeal) {
        // Logic to update deal using dealId and updatedDeal data
        return ResponseEntity.ok("Deal updated successfully");
    }

    @DeleteMapping("/{dealId}")
    public ResponseEntity<String> endDeal(@PathVariable int dealId) {
        // Logic to end a deal by dealId
        return ResponseEntity.ok("Deal ended successfully");
    }

    // Other API endpoints for additional functionalities
}





@Service
public class DealServiceImpl implements DealService {
    private static final Logger LOGGER = LoggerFactory.getLogger(DealServiceImpl.class);

    @Autowired
    private DealRepository dealRepository;

    @Autowired
    private PurchaseHistoryRepository purchaseHistoryRepository;

    @Override
    @Transactional
    public Deal createDeal(Deal deal) {
        validateDealInput(deal);
        try {
            return dealRepository.save(deal);
        } catch (Exception e) {
            LOGGER.error("Error occurred while creating deal: {}", e.getMessage());
            throw new RuntimeException("Failed to create deal");
        }
    }

    // Other methods

    private void validateDealInput(Deal deal) {
        // Implement input validation for Deal object
        // For example, check if price, start time, and end time are valid
        if (deal.getPrice() <= 0 || deal.getStartTime().isAfter(deal.getEndTime())) {
            throw new IllegalArgumentException("Invalid deal details");
        }
    }
}




@RestController
@RequestMapping("/deals")
public class DealController {
    private final DealService dealService;

    @Autowired
    public DealController(DealService dealService) {
        this.dealService = dealService;
    }

    @PostMapping
    public ResponseEntity<Deal> createDeal(@RequestBody Deal deal) {
        Deal createdDeal = dealService.createDeal(deal);
        return ResponseEntity.ok(createdDeal);
    }

    // Other endpoints

    @PutMapping("/{dealId}")
    public ResponseEntity<String> updateDeal(@PathVariable int dealId, @RequestBody Deal updatedDeal) {
        if (updatedDeal.getDealId() != dealId) {
            return ResponseEntity.badRequest().body("Deal ID mismatch");
        }
        // Implement update logic
        return ResponseEntity.ok("Deal updated successfully");
    }

    @DeleteMapping("/{dealId}")
    public ResponseEntity<String> endDeal(@PathVariable int dealId) {
        // Implement end deal logic
        return ResponseEntity.ok("Deal ended successfully");
    }
}



@Service
public class DealServiceImpl implements DealService {
    private static final Logger LOGGER = LoggerFactory.getLogger(DealServiceImpl.class);

    @Autowired
    private DealRepository dealRepository;

    @Autowired
    private PurchaseHistoryRepository purchaseHistoryRepository;

    @Override
    @Transactional
    public Deal createDeal(Deal deal) {
        validateDealInput(deal);
        try {
            return dealRepository.save(deal);
        } catch (Exception e) {
            LOGGER.error("Error occurred while creating deal: {}", e.getMessage());
            throw new RuntimeException("Failed to create deal");
        }
    }

    @Override
    public boolean userAlreadyPurchased(Deal deal, int userId) {
        return purchaseHistoryRepository.existsByUserIdAndDealId(userId, deal.getDealId());
    }

    @Override
    @Transactional
    public boolean buyDeal(int dealId, int userId) {
        Deal deal = dealRepository.findById(dealId).orElse(null);
        if (deal != null && canBuyDeal(deal, userId)) {
            deal.setItemsSold(deal.getItemsSold() + 1);
            dealRepository.save(deal);
            return true;
        }
        return false;
    }

    private boolean canBuyDeal(Deal deal, int userId) {
        LocalDateTime currentTime = LocalDateTime.now();
        return currentTime.isBefore(deal.getEndTime()) &&
               deal.getItemsSold() < deal.getItemsAvailable() &&
               !userAlreadyPurchased(deal, userId);
    }

    private void validateDealInput(Deal deal) {
        // Validation logic
    }
}
Leave a Comment