Ignore:
Timestamp:
10/01/22 22:57:41 (2 years ago)
Author:
Marko <Marko@…>
Branches:
master
Children:
47f4eaf
Parents:
fd5b100
Message:

Added few methods in PhoneOffer service

File:
1 edited

Legend:

Unmodified
Added
Removed
  • phonelux-backend/src/main/java/finki/it/phoneluxbackend/services/PhoneOfferService.java

    rfd5b100 rffd50db  
    55import finki.it.phoneluxbackend.repositories.PhoneOfferRepository;
    66import finki.it.phoneluxbackend.repositories.PhoneRepository;
     7import org.apache.coyote.Response;
    78import org.springframework.http.ResponseEntity;
    89import org.springframework.stereotype.Service;
     
    126127        List<PhoneOffer> offers = phoneOfferRepository.findAll();
    127128
    128         return offers.stream()
    129                 .filter(offer -> offer.getOffer_shop().equalsIgnoreCase(shop))
     129        if(shop.equalsIgnoreCase("mobigo"))
     130            shop = "Mobi Go";
     131
     132        if(shop.equalsIgnoreCase("mobilezone"))
     133            shop = "Mobile Zone";
     134        String finalShop = shop;
     135
     136        return offers.stream()
     137                .filter(offer -> offer.getOffer_shop().stripIndent().equalsIgnoreCase(finalShop.stripIndent()))
    130138                .collect(Collectors.toList());
    131139    }
     
    301309                .collect(Collectors.toList());
    302310    }
     311
     312    public ResponseEntity<Object> addOffer(PhoneOffer offer) {
     313        phoneOfferRepository.save(offer);
     314        return ResponseEntity.ok().build();
     315    }
     316
     317    public ResponseEntity<Object> deleteOffer(Long offerId) {
     318        boolean exists = phoneOfferRepository.existsById(offerId);
     319
     320        if(!exists)
     321            throw new IllegalStateException("Phone offer with id "+offerId+" does not exist");
     322
     323        phoneOfferRepository.deleteById(offerId);
     324
     325        return ResponseEntity.ok().build();
     326    }
     327
     328    public List<PhoneOffer> getAllOffers() {
     329        return phoneOfferRepository.findAll();
     330    }
     331
     332    public ResponseEntity<Object> addPhoneModelToOffer(Long offerId, Long phoneId) {
     333        boolean exists = phoneOfferRepository.existsById(offerId);
     334
     335        if(!exists)
     336            throw new IllegalStateException("Phone offer with id "+offerId+" does not exist");
     337
     338        PhoneOffer offer = phoneOfferRepository.findById(offerId).get();
     339
     340        exists = phoneRepository.existsById(phoneId);
     341
     342        if(!exists)
     343            throw new IllegalStateException("Phone with id "+phoneId+" does not exist");
     344
     345        Phone phone = phoneRepository.findById(phoneId).get();
     346
     347        offer.setPhone(phone);
     348        phoneOfferRepository.save(offer);
     349
     350        return ResponseEntity.ok().build();
     351    }
     352
     353    public ResponseEntity<Object> changePriceForOffer(Long offerId, int price) {
     354        boolean exists = phoneOfferRepository.existsById(offerId);
     355
     356        if(!exists)
     357            throw new IllegalStateException("Phone offer with id "+offerId+" does not exist");
     358
     359        PhoneOffer offer = phoneOfferRepository.findById(offerId).get();
     360        offer.setPrice(price);
     361        phoneOfferRepository.save(offer);
     362
     363        return ResponseEntity.ok().build();
     364    }
    303365}
Note: See TracChangeset for help on using the changeset viewer.