Changeset ffd50db for phonelux-backend


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

Location:
phonelux-backend/src/main/java/finki/it/phoneluxbackend
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • phonelux-backend/src/main/java/finki/it/phoneluxbackend/controllers/AdminController.java

    rfd5b100 rffd50db  
    2323            MediaType.APPLICATION_XML_VALUE
    2424    })
    25     public ResponseEntity<Object> editOffer(@PathVariable("offerId") Long offerId, @RequestBody PhoneOffer editedOffer){
     25    public ResponseEntity<Object> editOffer(@PathVariable("offerId") Long offerId, @RequestBody PhoneOffer editedOffer)
     26    {
    2627
    2728        return phoneOfferService.editOffer(offerId,editedOffer);
  • phonelux-backend/src/main/java/finki/it/phoneluxbackend/controllers/PhoneController.java

    rfd5b100 rffd50db  
    66import finki.it.phoneluxbackend.services.PhoneService;
    77import lombok.AllArgsConstructor;
     8import org.apache.coyote.Response;
    89import org.springframework.beans.factory.annotation.Autowired;
     10import org.springframework.http.MediaType;
     11import org.springframework.http.ResponseEntity;
    912import org.springframework.web.bind.annotation.*;
    1013
     
    5053    }
    5154
     55    @GetMapping(path = "/totaloffers/{phoneModel}")
     56    public Long getTotalOffersForPhone(@PathVariable("phoneModel") String phoneModel){
     57        return phoneService.getTotalOffersForPhone(phoneModel);
     58    }
     59
     60    @PutMapping(path = "/settotaloffers/{phoneId}/{totaloffers}")
     61    public ResponseEntity<Object> setTotalOffersForPhone(@PathVariable("phoneId") Long phoneId,
     62                                                         @PathVariable("totaloffers") int totaloffers){
     63        return phoneService.setTotalOffersForPhone(phoneId,totaloffers);
     64    }
     65
     66    @PutMapping(path = "/setlowestprice/{phoneId}/{lowestPrice}")
     67    public ResponseEntity<Object> setLowestPriceForPhone(@PathVariable("phoneId") Long phoneId,
     68                                                         @PathVariable("lowestPrice") int lowestPrice){
     69        return phoneService.setLowestPriceForPhone(phoneId,lowestPrice);
     70    }
     71
     72    @PutMapping(path = "/setimageurl/{phoneId}", consumes = {
     73            MediaType.APPLICATION_JSON_VALUE,
     74            MediaType.APPLICATION_XML_VALUE
     75    }, produces = {
     76            MediaType.APPLICATION_JSON_VALUE,
     77            MediaType.APPLICATION_XML_VALUE
     78    })
     79    public ResponseEntity<Object> setImageUrlForPhone(@PathVariable("phoneId") Long phoneId,
     80                                                         @RequestBody String newImageUrl){
     81        return phoneService.setImageUrlForPhone(phoneId,newImageUrl);
     82    }
     83
     84
    5285}
  • phonelux-backend/src/main/java/finki/it/phoneluxbackend/controllers/PhoneOfferController.java

    rfd5b100 rffd50db  
    77import lombok.AllArgsConstructor;
    88import org.springframework.beans.factory.annotation.Autowired;
     9import org.springframework.http.ResponseEntity;
    910import org.springframework.web.bind.annotation.*;
    1011
     
    1617    private final PhoneOfferService phoneOfferService;
    1718
     19
     20    @GetMapping(path = "/alloffers")
     21    public List<PhoneOffer> getAllOffers(){
     22        return phoneOfferService.getAllOffers();
     23    }
    1824    @GetMapping(path = "/phones/offers/{phoneId}")
    1925    public List<PhoneOffer> getOffersForPhone(@PathVariable("phoneId") Long phoneId){
     
    2228
    2329    @GetMapping(path = "/multipleoffers")
    24     public List<PhoneOffer> getPhoneOffer(@RequestParam("offerIds") String offerIds){
     30    public List<PhoneOffer> getMultiplePhoneOffers(@RequestParam("offerIds") String offerIds){
    2531        return phoneOfferService.getMultiplePhoneOffers(offerIds);
    2632    }
     
    5864    }
    5965
     66
     67    @PutMapping(path = "/phoneoffer/{offerId}/addphonemodel/{phoneId}")
     68    public ResponseEntity<Object> addPhoneModelToOffer(@PathVariable("offerId") Long offerId,
     69                                                       @PathVariable("phoneId") Long phoneId)
     70    {
     71        return phoneOfferService.addPhoneModelToOffer(offerId,phoneId);
     72    }
     73    @PutMapping(path = "/phoneoffer/{offerId}/changeprice/{price}")
     74    public ResponseEntity<Object> changePriceForOffer(@PathVariable("offerId") Long offerId,
     75                                                       @PathVariable("price") int price)
     76    {
     77        return phoneOfferService.changePriceForOffer(offerId,price);
     78    }
     79
     80    @PostMapping(path = "/phoneoffer/addoffer")
     81    public ResponseEntity<Object> addOffer(@RequestBody PhoneOffer offer)
     82    {
     83        return phoneOfferService.addOffer(offer);
     84    }
     85
     86    @DeleteMapping(path = "/phoneoffer/deleteoffer/{offerId}")
     87    public ResponseEntity<Object> deleteOffer(@PathVariable("offerId") Long offerId)
     88    {
     89        return phoneOfferService.deleteOffer(offerId);
     90    }
    6091}
  • phonelux-backend/src/main/java/finki/it/phoneluxbackend/entities/Phone.java

    rfd5b100 rffd50db  
    4343    }
    4444
    45 
     45    public Phone(Long id, String brand, String model, String image_url, Integer total_offers, Integer lowestPrice) {
     46        this.id = id;
     47        this.brand = brand;
     48        this.model = model;
     49        this.image_url = image_url;
     50        this.total_offers = total_offers;
     51        this.lowestPrice = lowestPrice;
     52    }
    4653}
  • phonelux-backend/src/main/java/finki/it/phoneluxbackend/entities/PhoneOffer.java

    rfd5b100 rffd50db  
    2020    @Id
    2121    @Column(name = "offer_id")
     22    @GeneratedValue(strategy=GenerationType.IDENTITY)
    2223    private Long id;
    2324
     
    115116    }
    116117
     118    public PhoneOffer(String offer_shop,
     119                      String offer_name,
     120                      Integer price,
     121                      String ram_memory,
     122                      String rom_memory,
     123                      String color,
     124                      String front_camera,
     125                      String back_camera,
     126                      String chipset,
     127                      String battery,
     128                      String operating_system,
     129                      String cpu,
     130                      String image_url,
     131                      String offer_url,
     132                      Date last_updated,
     133                      Boolean is_validated,
     134                      String offer_description,
     135                      String offer_shop_code) {
     136        this.offer_shop = offer_shop;
     137        this.offer_name = offer_name;
     138        this.price = price;
     139        this.ram_memory = ram_memory;
     140        this.rom_memory = rom_memory;
     141        this.color = color;
     142        this.front_camera = front_camera;
     143        this.back_camera = back_camera;
     144        this.chipset = chipset;
     145        this.battery = battery;
     146        this.operating_system = operating_system;
     147        this.cpu = cpu;
     148        this.image_url = image_url;
     149        this.offer_url = offer_url;
     150        this.last_updated = last_updated;
     151        this.is_validated = is_validated;
     152        this.offer_description = offer_description;
     153        this.offer_shop_code = offer_shop_code;
     154    }
     155
    117156    public PhoneOffer(Long id,
    118157                      String offer_shop,
  • phonelux-backend/src/main/java/finki/it/phoneluxbackend/repositories/PhoneRepository.java

    rfd5b100 rffd50db  
    55import org.springframework.stereotype.Repository;
    66
     7import java.util.Optional;
     8
    79@Repository
    810public interface PhoneRepository extends JpaRepository<Phone,Long> {
    9 
     11    Optional<Phone> findPhoneByModel(String model);
    1012}
  • 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}
  • phonelux-backend/src/main/java/finki/it/phoneluxbackend/services/PhoneService.java

    rfd5b100 rffd50db  
    22
    33import finki.it.phoneluxbackend.entities.Phone;
    4 import finki.it.phoneluxbackend.entities.PhoneOffer;
    54import finki.it.phoneluxbackend.repositories.PhoneRepository;
    6 import org.springframework.data.domain.PageRequest;
    7 import org.springframework.data.domain.Sort;
     5import org.springframework.http.ResponseEntity;
    86import org.springframework.stereotype.Service;
    97
     
    2725                                 String ram, String rom, String frontcamera, String backcamera, String chipset,
    2826                                 String cpu, String operatingsystem, String color, String battery){
    29         List<Phone> phones = phoneRepository.findAll();
     27        List<Phone> phones = phoneRepository.findAll().stream()
     28                .filter(phone -> phone.getTotal_offers() > 0)
     29                .collect(Collectors.toList());
    3030
    3131
     
    153153        }
    154154
    155 
    156155        phones = phones.stream().sorted(Comparator.comparing(Phone::getTotal_offers).reversed())
    157156                .collect(Collectors.toList());
     
    228227    public List<String> getBrands(){
    229228        return phoneRepository.findAll().stream()
    230                 .map(Phone::getBrand).distinct()
     229                .map(phone -> phone.getBrand().stripIndent())
     230                .distinct()
    231231                .collect(Collectors.toList());
    232232    }
     
    238238        return phoneRepository.findById(phoneId).get();
    239239    }
     240
     241    public Long getTotalOffersForPhone(String phoneModel) {
     242        String model = String.join(" ", phoneModel.split("\\*"));
     243        boolean exists = phoneRepository.findPhoneByModel(model).isPresent();
     244
     245        if(!exists)
     246            throw new IllegalStateException("Phone with model "+model+" does not exist");
     247
     248        return phoneRepository.findPhoneByModel(model).get().getPhoneOffers().stream().count();
     249    }
     250
     251    public ResponseEntity<Object> setTotalOffersForPhone(Long phoneId, int totaloffers) {
     252        boolean exists = phoneRepository.findById(phoneId).isPresent();
     253
     254        if(!exists)
     255            throw new IllegalStateException("Phone with id "+phoneId+" does not exist");
     256
     257        Phone phone = phoneRepository.findById(phoneId).get();
     258        phone.setTotal_offers(totaloffers);
     259        phoneRepository.save(phone);
     260        return ResponseEntity.ok().build();
     261    }
     262
     263    public ResponseEntity<Object> setLowestPriceForPhone(Long phoneId, int lowestPrice) {
     264        boolean exists = phoneRepository.findById(phoneId).isPresent();
     265
     266        if(!exists)
     267            throw new IllegalStateException("Phone with id "+phoneId+" does not exist");
     268
     269        Phone phone = phoneRepository.findById(phoneId).get();
     270        phone.setLowestPrice(lowestPrice);
     271        phoneRepository.save(phone);
     272        return ResponseEntity.ok().build();
     273    }
     274
     275
     276    public ResponseEntity<Object> setImageUrlForPhone(Long phoneId, String newImageUrl) {
     277        boolean exists = phoneRepository.findById(phoneId).isPresent();
     278
     279        if(!exists)
     280            throw new IllegalStateException("Phone with id "+phoneId+" does not exist");
     281
     282        Phone phone = phoneRepository.findById(phoneId).get();
     283        phone.setImage_url(newImageUrl);
     284        phoneRepository.save(phone);
     285
     286        return ResponseEntity.ok().build();
     287    }
    240288}
Note: See TracChangeset for help on using the changeset viewer.