[ce6ad22] | 1 | package parkup.services;
|
---|
| 2 |
|
---|
| 3 | import java.util.List;
|
---|
| 4 | import java.util.Objects;
|
---|
| 5 | import java.util.Optional;
|
---|
| 6 | import java.util.UUID;
|
---|
| 7 | import javax.transaction.Transactional;
|
---|
| 8 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
| 9 | import org.springframework.stereotype.Service;
|
---|
| 10 | import parkup.entities.Vraboten;
|
---|
| 11 | import parkup.repositories.VrabotenRepository;
|
---|
| 12 |
|
---|
| 13 | @Service
|
---|
| 14 | public class VrabotenService {
|
---|
| 15 | private final VrabotenRepository vrabotenRepository;
|
---|
| 16 |
|
---|
| 17 | @Autowired
|
---|
| 18 | public VrabotenService(VrabotenRepository vrabotenRepository) {
|
---|
| 19 | this.vrabotenRepository = vrabotenRepository;
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | public List<Vraboten> getVraboteni() {
|
---|
| 23 | return this.vrabotenRepository.findAll();
|
---|
| 24 | }
|
---|
| 25 |
|
---|
| 26 | public Vraboten findById(int vrabotenId) {
|
---|
| 27 | Optional<Vraboten> vraboten = Optional.ofNullable(this.vrabotenRepository.findByVrabotenId(vrabotenId));
|
---|
| 28 | return vraboten.orElse(null);
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | public void addVraboten(Vraboten vraboten) {
|
---|
| 32 | Optional<Vraboten> vrabotenOpt = this.vrabotenRepository.findVrabotenByEmail(vraboten.getEmail());
|
---|
| 33 | if (vrabotenOpt.isPresent()) {
|
---|
| 34 | throw new IllegalStateException("Email already taken, try adding a vraboten with a different valid email address");
|
---|
| 35 | } else {
|
---|
| 36 | System.out.println(vraboten);
|
---|
| 37 | this.vrabotenRepository.save(vraboten);
|
---|
| 38 | }
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | @Transactional
|
---|
| 42 | public void updateVraboten(int vrabotenId, String firstName, String lastName, String mobile, String email) {
|
---|
| 43 | Optional<Vraboten> vrabotenOpt = Optional.ofNullable(this.vrabotenRepository.findByVrabotenId(vrabotenId));
|
---|
| 44 | if (vrabotenOpt.isPresent()) {
|
---|
| 45 | Vraboten vrabotenNov = this.vrabotenRepository.findByVrabotenId(vrabotenId);
|
---|
| 46 | if (email != null && email.length() > 1 && email.contains("@") && !Objects.equals(vrabotenNov.getEmail(), email)) {
|
---|
| 47 | Optional<Vraboten> userOpt1 = this.vrabotenRepository.findVrabotenByEmail(email);
|
---|
| 48 | if (userOpt1.isPresent()) {
|
---|
| 49 | throw new IllegalStateException("email taken");
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | vrabotenNov.setEmail(email);
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | if (firstName != null && firstName.length() > 1 && !Objects.equals(vrabotenNov.getFirstName(), firstName)) {
|
---|
| 56 | vrabotenNov.setFirstName(firstName);
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | if (lastName != null && lastName.length() > 1 && !Objects.equals(vrabotenNov.getLastName(), lastName)) {
|
---|
| 60 | vrabotenNov.setLastName(lastName);
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | if (mobile != null && mobile.length() > 0 && !Objects.equals(vrabotenNov.getMobile(), mobile)) {
|
---|
| 64 | vrabotenNov.setMobile(mobile);
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | public void deleteVraboten(int vrabotenId) {
|
---|
| 70 | Optional<Vraboten> vrabotenOpt = Optional.ofNullable(this.vrabotenRepository.findByVrabotenId(vrabotenId));
|
---|
| 71 | if (vrabotenOpt.isPresent()) {
|
---|
| 72 | this.vrabotenRepository.deleteByVrabotenId(vrabotenId);
|
---|
| 73 | } else {
|
---|
| 74 | throw new IllegalStateException("Vraboten doesn't exist, therefore can't be deleted");
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
| 77 | } |
---|