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/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.