Changeset 1413ee2 for src/main/java
- Timestamp:
- 01/03/24 00:00:07 (11 months ago)
- Branches:
- master
- Children:
- 53765dd
- Parents:
- 77205be
- 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 3 3 import edu.gjoko.schedlr.entity.Stakeholder; 4 4 import edu.gjoko.schedlr.services.StakeholderService; 5 import org.springframework.beans.factory.annotation.Autowired;6 import org.springframework.http.MediaType;7 5 import org.springframework.stereotype.Controller; 8 6 import org.springframework.ui.Model; -
src/main/java/edu/gjoko/schedlr/controllers/rest/BusinessApi.java
r77205be r1413ee2 27 27 28 28 @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); 31 32 } 32 33 -
src/main/java/edu/gjoko/schedlr/controllers/rest/CustomerApi.java
r77205be r1413ee2 24 24 25 25 @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); 28 29 } 29 30 } -
src/main/java/edu/gjoko/schedlr/entity/Service.java
r77205be r1413ee2 54 54 private List<Appointment> appointments; 55 55 56 @Column(name = "service_status") 57 @Enumerated(EnumType.STRING) 58 private ServiceStatus serviceStatus; 59 56 60 @Column(name = "created") 57 61 @CreatedDate -
src/main/java/edu/gjoko/schedlr/repositories/BusinessRepository.java
r77205be r1413ee2 16 16 Business findBusinessByOwner(Stakeholder owner); 17 17 18 List<Business> findBusinessesByBusinessTypeAndBusinessStatus(BusinessType businessType, BusinessStatus businessStatus); 19 18 Business findBusinessesByOwner_Id(Long ownerId); 20 19 List<Business> findBusinessesByBusinessStatusAndBusinessType_Id(BusinessStatus businessStatus, Long id); 21 20 } -
src/main/java/edu/gjoko/schedlr/repositories/ServiceRepository.java
r77205be r1413ee2 1 1 package edu.gjoko.schedlr.repositories; 2 2 3 import edu.gjoko.schedlr.entity.Business;4 3 import edu.gjoko.schedlr.entity.Service; 5 4 import org.springframework.data.jpa.repository.JpaRepository; 5 import org.springframework.data.jpa.repository.Modifying; 6 import org.springframework.data.jpa.repository.Query; 6 7 import org.springframework.stereotype.Repository; 8 9 import java.util.List; 7 10 8 11 @Repository -
src/main/java/edu/gjoko/schedlr/repositories/ServiceTypeRepository.java
r77205be r1413ee2 4 4 import edu.gjoko.schedlr.entity.ServiceType; 5 5 import org.springframework.data.jpa.repository.JpaRepository; 6 import org.springframework.data.jpa.repository.Modifying; 7 import org.springframework.data.jpa.repository.Query; 6 8 import org.springframework.stereotype.Repository; 7 9 -
src/main/java/edu/gjoko/schedlr/services/BusinessService.java
r77205be r1413ee2 1 1 package edu.gjoko.schedlr.services; 2 2 3 import edu.gjoko.schedlr.entity.*; 4 import edu.gjoko.schedlr.repositories.*; 3 import edu.gjoko.schedlr.entity.Business; 4 import edu.gjoko.schedlr.entity.Stakeholder; 5 import edu.gjoko.schedlr.entity.StakeholderType; 6 import edu.gjoko.schedlr.repositories.BusinessRepository; 5 7 import lombok.AllArgsConstructor; 6 8 import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 7 9 import org.springframework.stereotype.Service; 8 import org.springframework.util.CollectionUtils;9 10 10 11 import java.util.List; … … 18 19 19 20 private final BusinessRepository businessRepository; 20 private final ServiceTypeRepository serviceTypeRepository; 21 private final ServiceRepository serviceRepository; 21 private final ServicesService servicesService; 22 22 private final StakeholderService stakeholderService; 23 24 private final StakeholderRepository stakeholderRepository;25 23 private BCryptPasswordEncoder bCryptPasswordEncoder; 26 24 27 25 public void saveBusiness(Business business) { 28 s aveNewServiceTypes(business);26 servicesService.saveNewServiceTypes(business); 29 27 business.getOwner().setStakeholderType(StakeholderType.BUSINESS_OWNER); 30 28 business.getOwner().setPassword(bCryptPasswordEncoder.encode(business.getOwner().getPassword())); … … 33 31 } 34 32 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 47 33 public List<Business> findAll() { 48 34 return businessRepository.findAll(); 49 35 } 50 36 51 public void updateBusinesses(List<Business> businessList ) {37 public void updateBusinesses(List<Business> businessList, Long businessOwnerId) { 52 38 businessList 53 39 .stream() 54 40 .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); 58 52 }); 59 53 } … … 68 62 return businessRepository.findBusinessesByBusinessStatusAndBusinessType_Id(ACTIVE, businessTypeId); 69 63 } 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 }92 64 } -
src/main/java/edu/gjoko/schedlr/services/StakeholderService.java
r77205be r1413ee2 4 4 import edu.gjoko.schedlr.entity.StakeholderType; 5 5 import edu.gjoko.schedlr.repositories.StakeholderRepository; 6 import lombok.RequiredArgsConstructor; 6 7 import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; 7 8 import org.springframework.stereotype.Service; 8 9 9 import java.util.Optional;10 11 10 @Service 11 @RequiredArgsConstructor 12 12 public class StakeholderService { 13 13 14 private StakeholderRepository stakeholderRepository;15 private BCryptPasswordEncoder bCryptPasswordEncoder;14 private final StakeholderRepository stakeholderRepository; 15 private final BCryptPasswordEncoder bCryptPasswordEncoder; 16 16 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) { 24 18 stakeholder.setPassword(bCryptPasswordEncoder.encode(stakeholder.getPassword())); 25 19 stakeholder.setStakeholderType(StakeholderType.CUSTOMER); 26 returnstakeholderRepository.save(stakeholder);20 stakeholderRepository.save(stakeholder); 27 21 } 28 22 … … 31 25 } 32 26 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); 43 35 } 44 36 }
Note:
See TracChangeset
for help on using the changeset viewer.