Ignore:
Timestamp:
01/03/24 00:00:07 (11 months ago)
Author:
gjoko kostadinov <gjokokostadinov@…>
Branches:
master
Children:
53765dd
Parents:
77205be
Message:

Add all bug fixes.

Location:
src/main/java/edu/gjoko/schedlr
Files:
4 added
9 edited

Legend:

Unmodified
Added
Removed
  • src/main/java/edu/gjoko/schedlr/controllers/RegisterController.java

    r77205be r1413ee2  
    33import edu.gjoko.schedlr.entity.Stakeholder;
    44import edu.gjoko.schedlr.services.StakeholderService;
    5 import org.springframework.beans.factory.annotation.Autowired;
    6 import org.springframework.http.MediaType;
    75import org.springframework.stereotype.Controller;
    86import org.springframework.ui.Model;
  • src/main/java/edu/gjoko/schedlr/controllers/rest/BusinessApi.java

    r77205be r1413ee2  
    2727
    2828    @PatchMapping
    29     public void updateBusinesses(@RequestBody List<Business> businessList) {
    30         businessService.updateBusinesses(businessList);
     29    public void updateBusinesses(@RequestBody List<Business> businessList, HttpServletRequest request) {
     30        Long businessOwnerId = (long) request.getSession(true).getAttribute("stakeholderId");
     31        businessService.updateBusinesses(businessList, businessOwnerId);
    3132    }
    3233
  • src/main/java/edu/gjoko/schedlr/controllers/rest/CustomerApi.java

    r77205be r1413ee2  
    2424
    2525    @PatchMapping
    26     public void updateCustomer(@RequestBody Stakeholder stakeholder) {
    27         stakeholderService.saveOrUpdateStakeholder(stakeholder);
     26    public void updateCustomer(@RequestBody Stakeholder stakeholder, HttpServletRequest request) {
     27        Long customerId = (long) request.getSession(true).getAttribute("stakeholderId");
     28        stakeholderService.saveOrUpdateStakeholder(stakeholder, customerId);
    2829    }
    2930}
  • src/main/java/edu/gjoko/schedlr/entity/Service.java

    r77205be r1413ee2  
    5454    private List<Appointment> appointments;
    5555
     56    @Column(name = "service_status")
     57    @Enumerated(EnumType.STRING)
     58    private ServiceStatus serviceStatus;
     59
    5660    @Column(name = "created")
    5761    @CreatedDate
  • src/main/java/edu/gjoko/schedlr/repositories/BusinessRepository.java

    r77205be r1413ee2  
    1616    Business findBusinessByOwner(Stakeholder owner);
    1717
    18     List<Business> findBusinessesByBusinessTypeAndBusinessStatus(BusinessType businessType, BusinessStatus businessStatus);
    19 
     18    Business findBusinessesByOwner_Id(Long ownerId);
    2019    List<Business> findBusinessesByBusinessStatusAndBusinessType_Id(BusinessStatus businessStatus, Long id);
    2120}
  • src/main/java/edu/gjoko/schedlr/repositories/ServiceRepository.java

    r77205be r1413ee2  
    11package edu.gjoko.schedlr.repositories;
    22
    3 import edu.gjoko.schedlr.entity.Business;
    43import edu.gjoko.schedlr.entity.Service;
    54import org.springframework.data.jpa.repository.JpaRepository;
     5import org.springframework.data.jpa.repository.Modifying;
     6import org.springframework.data.jpa.repository.Query;
    67import org.springframework.stereotype.Repository;
     8
     9import java.util.List;
    710
    811@Repository
  • src/main/java/edu/gjoko/schedlr/repositories/ServiceTypeRepository.java

    r77205be r1413ee2  
    44import edu.gjoko.schedlr.entity.ServiceType;
    55import org.springframework.data.jpa.repository.JpaRepository;
     6import org.springframework.data.jpa.repository.Modifying;
     7import org.springframework.data.jpa.repository.Query;
    68import org.springframework.stereotype.Repository;
    79
  • src/main/java/edu/gjoko/schedlr/services/BusinessService.java

    r77205be r1413ee2  
    11package edu.gjoko.schedlr.services;
    22
    3 import edu.gjoko.schedlr.entity.*;
    4 import edu.gjoko.schedlr.repositories.*;
     3import edu.gjoko.schedlr.entity.Business;
     4import edu.gjoko.schedlr.entity.Stakeholder;
     5import edu.gjoko.schedlr.entity.StakeholderType;
     6import edu.gjoko.schedlr.repositories.BusinessRepository;
    57import lombok.AllArgsConstructor;
    68import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
    79import org.springframework.stereotype.Service;
    8 import org.springframework.util.CollectionUtils;
    910
    1011import java.util.List;
     
    1819
    1920    private final BusinessRepository businessRepository;
    20     private final ServiceTypeRepository serviceTypeRepository;
    21     private final ServiceRepository serviceRepository;
     21    private final ServicesService servicesService;
    2222    private final StakeholderService stakeholderService;
    23 
    24     private final StakeholderRepository stakeholderRepository;
    2523    private BCryptPasswordEncoder bCryptPasswordEncoder;
    2624
    2725    public void saveBusiness(Business business) {
    28         saveNewServiceTypes(business);
     26        servicesService.saveNewServiceTypes(business);
    2927        business.getOwner().setStakeholderType(StakeholderType.BUSINESS_OWNER);
    3028        business.getOwner().setPassword(bCryptPasswordEncoder.encode(business.getOwner().getPassword()));
     
    3331    }
    3432
    35     public void saveNewServiceTypes(Business business) {
    36         business.getServices()
    37                 .stream()
    38                 .forEach(service -> {
    39                     if (service.getServiceType().getId() == null) {
    40                         ServiceType serviceType = service.getServiceType();
    41                         serviceType.setBusinessType(business.getBusinessType());
    42                         service.setServiceType(serviceTypeRepository.save(serviceType));
    43                     }
    44                 });
    45     }
    46 
    4733    public List<Business> findAll() {
    4834        return businessRepository.findAll();
    4935    }
    5036
    51     public void updateBusinesses(List<Business> businessList) {
     37    public void updateBusinesses(List<Business> businessList, Long businessOwnerId) {
    5238        businessList
    5339                .stream()
    5440                .forEach(business -> {
    55                     stakeholderService.saveOrUpdateStakeholder(business.getOwner());
    56                     saveOrUpdateServices(business.getServices());
    57                     saveOrUpdateBusiness(business);
     41                    stakeholderService.saveOrUpdateStakeholder(business.getOwner(), businessOwnerId);
     42                });
     43    }
     44
     45    public void updateBusinessStatuses(List<Business> businessList) {
     46        businessList
     47                .stream()
     48                .forEach(business -> {
     49                    Business business1 = businessRepository.findById(business.getId()).get();
     50                    business1.setBusinessStatus(business.getBusinessStatus());
     51                    businessRepository.save(business1);
    5852                });
    5953    }
     
    6862        return businessRepository.findBusinessesByBusinessStatusAndBusinessType_Id(ACTIVE, businessTypeId);
    6963    }
    70 
    71 
    72 
    73     private void saveOrUpdateBusiness(Business business) {
    74         if (business.getId() != null) {
    75             var foundBusinessEntity = businessRepository.findById(business.getId());
    76             business.setCreated(foundBusinessEntity.get().getCreated());
    77         }
    78         businessRepository.save(business);
    79     }
    80 
    81     private void saveOrUpdateServices(List<edu.gjoko.schedlr.entity.Service> serviceList) {
    82         if (!CollectionUtils.isEmpty(serviceList)) {
    83             serviceList.forEach( service -> {
    84                 if (service.getId() != null) {
    85                     var found = serviceRepository.findById(service.getId());
    86                     service.setCreated(found.get().getCreated());
    87                 }
    88             });
    89             serviceRepository.saveAll(serviceList);
    90         }
    91     }
    9264}
  • src/main/java/edu/gjoko/schedlr/services/StakeholderService.java

    r77205be r1413ee2  
    44import edu.gjoko.schedlr.entity.StakeholderType;
    55import edu.gjoko.schedlr.repositories.StakeholderRepository;
     6import lombok.RequiredArgsConstructor;
    67import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
    78import org.springframework.stereotype.Service;
    89
    9 import java.util.Optional;
    10 
    1110@Service
     11@RequiredArgsConstructor
    1212public class StakeholderService {
    1313
    14     private StakeholderRepository stakeholderRepository;
    15     private BCryptPasswordEncoder bCryptPasswordEncoder;
     14    private final StakeholderRepository stakeholderRepository;
     15    private final BCryptPasswordEncoder bCryptPasswordEncoder;
    1616
    17     public StakeholderService(StakeholderRepository stakeholderRepository,
    18                               BCryptPasswordEncoder bCryptPasswordEncoder) {
    19         this.stakeholderRepository = stakeholderRepository;
    20         this.bCryptPasswordEncoder = bCryptPasswordEncoder;
    21     }
    22 
    23     public Stakeholder saveStakeholder(Stakeholder stakeholder) {
     17    public void saveStakeholder(Stakeholder stakeholder) {
    2418        stakeholder.setPassword(bCryptPasswordEncoder.encode(stakeholder.getPassword()));
    2519        stakeholder.setStakeholderType(StakeholderType.CUSTOMER);
    26         return stakeholderRepository.save(stakeholder);
     20        stakeholderRepository.save(stakeholder);
    2721    }
    2822
     
    3125    }
    3226
    33     public void saveOrUpdateStakeholder(Stakeholder stakeholder) {
    34         if (stakeholder.getId() != null) {
    35             var found = stakeholderRepository.findById(stakeholder.getId()).get();
    36             found.setFirstName(stakeholder.getFirstName());
    37             found.setLastName(stakeholder.getLastName());
    38             found.setPhoneNumber(stakeholder.getPhoneNumber());
    39             found.setEmail(stakeholder.getEmail());
    40             found.setUsername(stakeholder.getUsername());
    41             stakeholderRepository.save(found);
    42         }
     27    public void saveOrUpdateStakeholder(Stakeholder stakeholder, Long businessOwnerId) {
     28        var found = stakeholderRepository.findById(businessOwnerId).get();
     29        found.setFirstName(stakeholder.getFirstName());
     30        found.setLastName(stakeholder.getLastName());
     31        found.setPhoneNumber(stakeholder.getPhoneNumber());
     32        found.setEmail(stakeholder.getEmail());
     33        found.setUsername(stakeholder.getUsername());
     34        stakeholderRepository.save(found);
    4335    }
    4436}
Note: See TracChangeset for help on using the changeset viewer.