Ignore:
Timestamp:
12/26/23 18:50:43 (11 months ago)
Author:
gjoko kostadinov <gjokokostadinov@…>
Branches:
master
Children:
1413ee2
Parents:
950fa0d
Message:

Add entire code

Location:
src/main/java/edu/gjoko/schedlr
Files:
17 added
2 deleted
31 edited
4 moved

Legend:

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

    • Property mode changed from 100644 to 100755
  • src/main/java/edu/gjoko/schedlr/ServletInitializer.java

    • Property mode changed from 100644 to 100755
  • src/main/java/edu/gjoko/schedlr/config/AppAuthenticationEntryPoint.java

    • Property mode changed from 100644 to 100755
  • src/main/java/edu/gjoko/schedlr/config/AppAuthenticationFailureHandler.java

    • Property mode changed from 100644 to 100755
  • src/main/java/edu/gjoko/schedlr/config/AppConfig.java

    • Property mode changed from 100644 to 100755
    r950fa0d r77205be  
    1515    }
    1616
    17     @Bean
    18     public AuthenticationSuccessHandler appAuthenticationSuccessHandler() {
    19         return new AppAuthenticationSuccessHandler();
    20     }
    2117
    2218    @Bean
  • src/main/java/edu/gjoko/schedlr/config/AppFilter.java

    • Property mode changed from 100644 to 100755
    r950fa0d r77205be  
    6464                            break;
    6565                        case "CUSTOMER":
    66                             page = "/homepage";
     66                            if ("/customer_admin".equals(httpServletRequest.getRequestURI())) {
     67                                page = "/customer_admin";
     68                            } else {
     69                                page = "/homepage";
     70                            }
    6771                            break;
    6872                        case "BUSINESS_OWNER":
  • src/main/java/edu/gjoko/schedlr/config/AppSecurityConfig.java

    • Property mode changed from 100644 to 100755
    r950fa0d r77205be  
    3232    private final BCryptPasswordEncoder passwordEncoder;
    3333
    34     private final AuthenticationSuccessHandler authenticationSuccessHandler;
    35 
    3634    private final AuthenticationFailureHandler authenticationFailureHandler;
    3735
     
    5755                .loginPage("/login")
    5856                .loginProcessingUrl("/login")
    59                 .successHandler(authenticationSuccessHandler)
    6057                .failureHandler(authenticationFailureHandler)
    61                 .defaultSuccessUrl("/homepage")
     58                .defaultSuccessUrl("/login")
    6259                .and()
    6360                .logout(logout -> logout
  • src/main/java/edu/gjoko/schedlr/config/MvcConfig.java

    • Property mode changed from 100644 to 100755
    r950fa0d r77205be  
    1616        registry.addViewController("/admin").setViewName("admin");
    1717        registry.addViewController("/business_admin").setViewName("business_admin");
     18        registry.addViewController("/customer_admin").setViewName("customer_admin");
    1819    }
    1920}
  • src/main/java/edu/gjoko/schedlr/controllers/AdministrationController.java

    • Property mode changed from 100644 to 100755
    r950fa0d r77205be  
    88
    99import javax.servlet.http.HttpServletRequest;
    10 import java.security.Principal;
    1110
    1211@Controller
    13 public class AdminController {
     12public class AdministrationController {
    1413
    1514    @GetMapping(path = "/admin")
    16     public String getAdminPageTemplate(Model model, HttpServletRequest request) {
     15    public String getAdminPageTemplate(Model model) {
    1716        return "admin";
    1817    }
    1918
    2019    @GetMapping(path = "/business_admin")
    21     public String getBusinessAdminPageTemplate(Model model, HttpServletRequest request) {
    22         Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    23         String currentPrincipalName = authentication.getName();
     20    public String getBusinessAdminPageTemplate(Model model) {
     21        return "business_admin";
     22    }
    2423
    25         return "business_admin";
     24    @GetMapping(path = "/customer_admin")
     25    public String getCustomerAdminPageTemplate(Model model) {
     26        return "customer_admin";
    2627    }
    2728
  • src/main/java/edu/gjoko/schedlr/controllers/HomePageController.java

    • Property mode changed from 100644 to 100755
  • src/main/java/edu/gjoko/schedlr/controllers/LoginController.java

    • Property mode changed from 100644 to 100755
  • src/main/java/edu/gjoko/schedlr/controllers/RegisterController.java

    • Property mode changed from 100644 to 100755
  • src/main/java/edu/gjoko/schedlr/controllers/rest/BusinessApi.java

    • Property mode changed from 100644 to 100755
    r950fa0d r77205be  
    22
    33import edu.gjoko.schedlr.entity.Business;
    4 import edu.gjoko.schedlr.entity.BusinessType;
    54import edu.gjoko.schedlr.services.BusinessService;
    65import lombok.AllArgsConstructor;
     
    1312@RequestMapping("api/business")
    1413@AllArgsConstructor
    15 public class BusinessController {
     14public class BusinessApi {
    1615
    17     final BusinessService businessService;
     16    private final BusinessService businessService;
    1817
    1918    @PostMapping
    20     public void getBusinessTypes(@RequestBody Business business) {
     19    public void saveBusiness(@RequestBody Business business) {
    2120        businessService.saveBusiness(business);
    2221    }
     
    4039    @GetMapping(path = "/{businessTypeId}")
    4140    public List<Business> getBusinessesByBusinessType(@PathVariable("businessTypeId") Long id) {
    42         BusinessType businessType = new BusinessType();
    43         businessType.setId(id);
    44         return businessService.findByBusinessTypeAndActiveStatus(businessType);
     41        return businessService.findByBusinessTypeAndActiveStatus(id);
    4542    }
    4643}
  • src/main/java/edu/gjoko/schedlr/controllers/rest/NomenclatureApi.java

    • Property mode changed from 100644 to 100755
    r950fa0d r77205be  
    1313@RequestMapping("api/nomenclatures")
    1414@AllArgsConstructor
    15 public class NomenclatureController {
     15public class NomenclatureApi {
    1616
    1717    private final NomenclaturesService nomenclaturesService;
  • src/main/java/edu/gjoko/schedlr/controllers/rest/UserApi.java

    • Property mode changed from 100644 to 100755
    r950fa0d r77205be  
    11package edu.gjoko.schedlr.controllers.rest;
    22
    3 import edu.gjoko.schedlr.entity.Business;
    43import lombok.AllArgsConstructor;
    54import org.springframework.web.bind.annotation.GetMapping;
     
    87
    98import javax.servlet.http.HttpServletRequest;
    10 import java.util.List;
    119
    1210@RestController
    1311@RequestMapping("api/user")
    1412@AllArgsConstructor
    15 public class UserController {
     13public class UserApi {
    1614
    1715    @GetMapping(path = "/me")
  • src/main/java/edu/gjoko/schedlr/entity/Appointment.java

    • Property mode changed from 100644 to 100755
    r950fa0d r77205be  
    11package edu.gjoko.schedlr.entity;
    22
    3 import lombok.AllArgsConstructor;
    4 import lombok.Getter;
    5 import lombok.NoArgsConstructor;
    6 import lombok.Setter;
     3import com.fasterxml.jackson.annotation.JsonBackReference;
     4import lombok.*;
    75import org.springframework.data.annotation.LastModifiedDate;
    86import org.springframework.data.jpa.domain.support.AuditingEntityListener;
     
    1412@EntityListeners(AuditingEntityListener.class)
    1513@Table(name = "appointment")
    16 @Getter
    17 @Setter
     14@Data
    1815@NoArgsConstructor
    1916@AllArgsConstructor
     
    3027    private LocalDateTime endTime;
    3128
    32     @OneToOne
    33     @JoinColumn(name = "customer_id", referencedColumnName = "id")
     29    @ManyToOne
     30    @JoinColumn(name = "stakeholder_id")
     31    @JsonBackReference(value = "customerAppointments")
    3432    private Stakeholder customer;
    3533
    3634    @ManyToOne
    37     @JoinColumn(name = "business_id")
    38     private Business business;
     35    @JoinColumn(name = "service_id")
     36    @JsonBackReference(value = "serviceAppointments")
     37    private Service service;
    3938
    40     @ManyToOne
    41     @JoinColumn(name = "service_id")
    42     private Service service;
     39    @Column(name = "appointment_status", length = 32, columnDefinition = "varchar(32) default 'NEW'")
     40    @Enumerated(EnumType.STRING)
     41    private AppointmentStatus appointmentStatus = AppointmentStatus.NEW;
    4342
    4443    @Column(name = "created")
     
    4948    @LastModifiedDate
    5049    private LocalDateTime modified;
     50
     51    public String getTimePeriod() {
     52        return startTime + " - " + endTime;
     53    }
     54
    5155}
  • src/main/java/edu/gjoko/schedlr/entity/Business.java

    • Property mode changed from 100644 to 100755
    r950fa0d r77205be  
    3737    private BusinessType businessType;
    3838
    39     @ManyToOne()
     39    @OneToOne(cascade = CascadeType.PERSIST)
    4040    @JoinColumn(name = "owner_id", referencedColumnName = "id", nullable = false)
    4141    @JsonProperty("owner")
     
    4343
    4444    @OneToMany(mappedBy = "business", cascade = CascadeType.PERSIST)
    45     @JsonManagedReference
     45    @JsonManagedReference(value = "services")
    4646    private List<Service> services;
    4747
  • src/main/java/edu/gjoko/schedlr/entity/BusinessStatus.java

    • Property mode changed from 100644 to 100755
  • src/main/java/edu/gjoko/schedlr/entity/BusinessType.java

    • Property mode changed from 100644 to 100755
  • src/main/java/edu/gjoko/schedlr/entity/Service.java

    • Property mode changed from 100644 to 100755
    r950fa0d r77205be  
    33import com.fasterxml.jackson.annotation.JsonBackReference;
    44import com.fasterxml.jackson.annotation.JsonIgnore;
     5import com.fasterxml.jackson.annotation.JsonManagedReference;
    56import lombok.AllArgsConstructor;
    67import lombok.Getter;
     
    1314import javax.persistence.*;
    1415import java.time.LocalDateTime;
     16import java.util.List;
    1517
    1618@Entity
     
    3335    private Integer price;
    3436
    35     @OneToOne(cascade = CascadeType.PERSIST)
     37    @Column(name = "cumulated_rating")
     38    private Float rating = 0.0f;
     39
     40    @Column(name = "reviews_count")
     41    private Integer reviewsCount = 0;
     42
     43    @OneToOne(cascade = CascadeType.MERGE)
    3644    @JoinColumn(name = "service_type_id", referencedColumnName = "id")
    3745    private ServiceType serviceType;
     
    3947    @ManyToOne
    4048    @JoinColumn(name = "business_id")
    41     @JsonBackReference
     49    @JsonBackReference(value = "services")
    4250    private Business business;
     51
     52    @OneToMany(mappedBy="service")
     53    @JsonManagedReference(value = "serviceAppointments")
     54    private List<Appointment> appointments;
    4355
    4456    @Column(name = "created")
  • src/main/java/edu/gjoko/schedlr/entity/ServiceType.java

    • Property mode changed from 100644 to 100755
  • src/main/java/edu/gjoko/schedlr/entity/Stakeholder.java

    • Property mode changed from 100644 to 100755
    r950fa0d r77205be  
    22
    33import com.fasterxml.jackson.annotation.JsonIgnore;
     4import com.fasterxml.jackson.annotation.JsonManagedReference;
    45import lombok.AllArgsConstructor;
    56import lombok.Getter;
     
    1213import javax.persistence.*;
    1314import java.time.LocalDateTime;
     15import java.util.List;
    1416
    1517@Entity
     
    3941    private String email;
    4042
     43    @Column(name = "phone_number")
     44    private String phoneNumber;
     45
    4146    @Column(name = "username")
    4247    private String username;
     
    4449    @Column(name = "password")
    4550    private String password;
     51
     52    @OneToMany(mappedBy = "customer")
     53    @JsonManagedReference(value = "customerAppointments")
     54    private List<Appointment> appointments;
    4655
    4756    @Column(name = "created")
     
    5463    @JsonIgnore
    5564    private LocalDateTime modified;
     65
     66    public String getFullName() {
     67        return firstName + " " + lastName;
     68    }
    5669}
  • src/main/java/edu/gjoko/schedlr/entity/StakeholderType.java

    • Property mode changed from 100644 to 100755
  • src/main/java/edu/gjoko/schedlr/exceptions/BlockingTimeException.java

    • Property mode changed from 100644 to 100755
    r950fa0d r77205be  
    33public class BlockingTimeException extends RuntimeException {
    44
    5     public static final String MESSAGE = "The selected dates are overlapping with another appointment";
     5    public static final String MESSAGE = "Error! The selected dates are overlapping with another appointment.";
    66    public BlockingTimeException() {
    77        super(MESSAGE);
  • src/main/java/edu/gjoko/schedlr/repositories/AppointmentRepository.java

    • Property mode changed from 100644 to 100755
    r950fa0d r77205be  
    22
    33import edu.gjoko.schedlr.entity.Appointment;
    4 import edu.gjoko.schedlr.entity.Business;
    54import edu.gjoko.schedlr.entity.Stakeholder;
    65import org.springframework.data.jpa.repository.JpaRepository;
     
    109import java.time.LocalDateTime;
    1110import java.util.List;
     11import java.util.Optional;
    1212
    1313@Repository
    1414public interface AppointmentRepository extends JpaRepository<Appointment, Long> {
    1515
    16     List<Appointment> getAppointmentsByBusiness(Business business);
     16    @Query(value = "select ap from Appointment as ap " +
     17            "where ap.service.business.id = :businessId " +
     18            "and ap.appointmentStatus = 'NEW'")
     19    List<Appointment> getActiveAppointmentsByBusiness(Long businessId);
    1720
    18     List<Appointment> getAppointmentsByCustomer(Stakeholder customer);
     21    @Query( value = "select ap from Appointment as ap " +
     22            "where ap.service.business.id = :businessId " +
     23            "and (" +
     24            "(ap.startTime between :startDate and :endDate) " +
     25            "or (ap.endTime between :startDate and :endDate) " +
     26            "or (:startDate = ap.startTime and ap.startTime = :endDate)" +
     27            "or (:startDate > ap.startTime and ap.endTime > :endDate)" +
     28            ")")
     29    List<Appointment> findBlockingAppointments(Long businessId, LocalDateTime startDate, LocalDateTime endDate);
    1930
    20     List<Appointment> findAppointmentsByBusinessAndStartTimeBetweenOrEndTimeBetween(Business business, LocalDateTime startTime, LocalDateTime endTime, LocalDateTime startTime1, LocalDateTime endTime1);
     31    @Query(value = "select ap from Appointment  as ap " +
     32            "where ap.service.business.owner.id = :businessOwnerId " +
     33            "and ap.startTime > :now ")
     34    List<Appointment> findFutureAppointmentsByBusinessOwnerId(Long businessOwnerId, LocalDateTime now);
    2135
    22     @Query(value = "select a from Appointment a " +
    23             "where business_id = :businessId " +
    24             "and (" +
    25             "(start_time between :startDate and :endDate) " +
    26             "or (end_time between :startDate and :endDate) " +
    27             "or (:startDate <= start_time and end_time >= :endDate)" +
    28             ")", nativeQuery = true)
    29     List<Appointment> findBlockingAppointments(Long businessId, LocalDateTime startDate, LocalDateTime endDate);
     36    @Query(value = "select ap from Appointment  as ap " +
     37            "where ap.customer.id = :customerId " +
     38            "and ap.startTime < :now")
     39    List<Appointment> findPastAppointmentsByCustomerId(Long customerId, LocalDateTime now);
     40
     41    @Query(value = "select ap from Appointment  as ap " +
     42            "where ap.service.business.owner.id = :businessOwnerId " +
     43            "and ap.startTime < :now ")
     44    List<Appointment> findPastAppointmentsByBusinessOwnerId(Long businessOwnerId, LocalDateTime now);
     45
     46    @Query(value = "select ap from Appointment  as ap " +
     47            "where ap.customer.id = :customerId " +
     48            "and ap.startTime > :now ")
     49    List<Appointment> findFutureAppointmentsByCustomerId(Long customerId, LocalDateTime now);
     50
     51    Optional<Appointment> findAppointmentByIdAndCustomer_Id(Long id, Long customerId);
     52
     53    Optional<Appointment> findAppointmentByIdAndService_Business_Owner_Id(Long id, Long ownerId);
    3054}
  • src/main/java/edu/gjoko/schedlr/repositories/BusinessRepository.java

    • Property mode changed from 100644 to 100755
    r950fa0d r77205be  
    1717
    1818    List<Business> findBusinessesByBusinessTypeAndBusinessStatus(BusinessType businessType, BusinessStatus businessStatus);
     19
     20    List<Business> findBusinessesByBusinessStatusAndBusinessType_Id(BusinessStatus businessStatus, Long id);
    1921}
  • src/main/java/edu/gjoko/schedlr/repositories/BusinessTypeRepository.java

    • Property mode changed from 100644 to 100755
  • src/main/java/edu/gjoko/schedlr/repositories/ServiceRepository.java

    • Property mode changed from 100644 to 100755
  • src/main/java/edu/gjoko/schedlr/repositories/ServiceTypeRepository.java

    • Property mode changed from 100644 to 100755
  • src/main/java/edu/gjoko/schedlr/repositories/StakeholderRepository.java

    • Property mode changed from 100644 to 100755
  • src/main/java/edu/gjoko/schedlr/services/AppointmentsService.java

    • Property mode changed from 100644 to 100755
    r950fa0d r77205be  
    11package edu.gjoko.schedlr.services;
    22
    3 import edu.gjoko.schedlr.entity.Appointment;
    4 import edu.gjoko.schedlr.entity.Business;
    5 import edu.gjoko.schedlr.entity.Stakeholder;
     3import edu.gjoko.schedlr.dto.AppointmentInfoDto;
     4import edu.gjoko.schedlr.entity.*;
    65import edu.gjoko.schedlr.exceptions.BlockingTimeException;
     6import edu.gjoko.schedlr.mappers.AppointmentInfoDtoBusinessMapper;
     7import edu.gjoko.schedlr.mappers.AppointmentInfoDtoCustomerMapper;
    78import edu.gjoko.schedlr.repositories.AppointmentRepository;
    89import edu.gjoko.schedlr.repositories.ServiceRepository;
    9 import edu.gjoko.schedlr.repositories.ServiceTypeRepository;
     10import edu.gjoko.schedlr.repositories.StakeholderRepository;
    1011import lombok.AllArgsConstructor;
    1112import org.springframework.stereotype.Service;
    1213
     14import javax.persistence.EntityNotFoundException;
     15import javax.security.sasl.AuthenticationException;
     16import java.time.LocalDateTime;
     17import java.util.ArrayList;
    1318import java.util.List;
     19import java.util.Optional;
    1420
    1521@Service
     
    1824
    1925    private final AppointmentRepository appointmentRepository;
     26    private final StakeholderRepository stakeholderRepository;
     27    private final ServiceRepository serviceRepository;
     28    private final AppointmentInfoDtoBusinessMapper appointmentInfoDtoBusinessMapper;
     29    private final AppointmentInfoDtoCustomerMapper appointmentInfoDtoCustomerMapper;
    2030
    21     private final ServiceRepository serviceRepository;
    2231
    2332    public void saveAppointment(Appointment appointment) {
    2433        var service = serviceRepository.findById(appointment.getService().getId()).get();
    25         appointment.setEndTime(appointment.getStartTime().plusMinutes(service.getDuration()));
     34
     35        // remove 1 minute in order to prevent overlapping
     36        appointment.setEndTime(appointment.getStartTime().plusMinutes(service.getDuration() - 1));
    2637        List<Appointment> blockingAppointments = appointmentRepository.
    2738                findBlockingAppointments(
    28                         appointment.getBusiness().getId(), appointment.getStartTime(), appointment.getEndTime());
     39                        appointment.getService().getBusiness().getId(), appointment.getStartTime(), appointment.getEndTime());
    2940
    3041        // check to see if there are blocking exceptions
     
    3546    }
    3647
    37     public List<Appointment> getAppointmentsByBusiness(Business business) {
    38         return appointmentRepository.getAppointmentsByBusiness(business);
     48    public List<AppointmentInfoDto> getFutureAppointmentInfoList(Long stakeholderId) {
     49        StakeholderType type = stakeholderRepository.findById(stakeholderId).get().getStakeholderType();
     50        List<AppointmentInfoDto> appointmentInfoDtoList = new ArrayList<>();
     51
     52        switch (type) {
     53            case BUSINESS_OWNER -> appointmentInfoDtoList = appointmentRepository.findFutureAppointmentsByBusinessOwnerId(stakeholderId, LocalDateTime.now())
     54                    .stream()
     55                    .map(appointmentInfoDtoBusinessMapper::appointmentToAppointmentInfoDto)
     56                    .toList();
     57            case CUSTOMER -> appointmentInfoDtoList = appointmentRepository.findFutureAppointmentsByCustomerId(stakeholderId, LocalDateTime.now())
     58                    .stream()
     59                    .map(appointmentInfoDtoCustomerMapper::appointmentToAppointmentInfoDto)
     60                    .toList();
     61        }
     62
     63        return appointmentInfoDtoList;
    3964    }
    4065
    41     private List<Appointment> getAppointmentsByCustomer(Stakeholder stakeholder) {
    42         return appointmentRepository.getAppointmentsByCustomer(stakeholder);
     66    public List<AppointmentInfoDto> getPastAppointmentInfoList(Long stakeholderId) {
     67        StakeholderType type = stakeholderRepository.findById(stakeholderId).get().getStakeholderType();
     68        List<Appointment> appointmentInfoDtoList = new ArrayList<>();
     69
     70        switch (type) {
     71            case BUSINESS_OWNER -> appointmentInfoDtoList = appointmentRepository.findPastAppointmentsByBusinessOwnerId(stakeholderId, LocalDateTime.now());
     72            case CUSTOMER -> appointmentInfoDtoList = appointmentRepository.findPastAppointmentsByCustomerId(stakeholderId, LocalDateTime.now());
     73        }
     74
     75        return appointmentInfoDtoList
     76                .stream()
     77                .map(appointmentInfoDtoBusinessMapper::appointmentToAppointmentInfoDto)
     78                .toList();
     79
     80    }
     81
     82    public List<Appointment> getActiveAppointmentsByBusiness(Long businessId) {
     83        return appointmentRepository.getActiveAppointmentsByBusiness(businessId);
     84    }
     85
     86    public void deleteAppointment(Long appointmentId, Long stakeholderId) throws AuthenticationException {
     87        Optional<Stakeholder> stakeholderOptional = stakeholderRepository.findById(stakeholderId);
     88        if (stakeholderOptional.isPresent()) {
     89            Optional<Appointment> optional = appointmentRepository.findAppointmentByIdAndCustomer_Id(appointmentId, stakeholderId);
     90            if (optional.isEmpty()) {
     91                optional = appointmentRepository.findAppointmentByIdAndService_Business_Owner_Id(appointmentId, stakeholderId);
     92                if (optional.isEmpty()) {
     93                    throw new EntityNotFoundException("No appointment was found for the give stakeholder and appointment id.");
     94                } else {
     95                    softDeleteAppointment(optional.get(), AppointmentStatus.CANCELLED_BY_BUSINESS_OWNER);
     96                }
     97            } else {
     98                softDeleteAppointment(optional.get(), AppointmentStatus.CANCELLED_BY_CUSTOMER);
     99            }
     100        } else {
     101            throw new AuthenticationException();
     102        }
     103    }
     104
     105    public void softDeleteAppointment(Appointment appointment, AppointmentStatus appointmentStatus) {
     106        appointment.setAppointmentStatus(appointmentStatus);
     107        appointmentRepository.save(appointment);
    43108    }
    44109}
  • src/main/java/edu/gjoko/schedlr/services/BusinessService.java

    • Property mode changed from 100644 to 100755
    r950fa0d r77205be  
    22
    33import edu.gjoko.schedlr.entity.*;
    4 import edu.gjoko.schedlr.repositories.BusinessRepository;
    5 import edu.gjoko.schedlr.repositories.ServiceRepository;
    6 import edu.gjoko.schedlr.repositories.ServiceTypeRepository;
    7 import edu.gjoko.schedlr.repositories.StakeholderRepository;
     4import edu.gjoko.schedlr.repositories.*;
    85import lombok.AllArgsConstructor;
    96import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
    107import org.springframework.stereotype.Service;
     8import org.springframework.util.CollectionUtils;
    119
    1210import java.util.List;
     
    2220    private final ServiceTypeRepository serviceTypeRepository;
    2321    private final ServiceRepository serviceRepository;
     22    private final StakeholderService stakeholderService;
    2423
    2524    private final StakeholderRepository stakeholderRepository;
     
    5453                .stream()
    5554                .forEach(business -> {
    56                     stakeholderRepository.save(business.getOwner());
    57                     serviceRepository.saveAll(business.getServices());
    58                     businessRepository.save(business);
     55                    stakeholderService.saveOrUpdateStakeholder(business.getOwner());
     56                    saveOrUpdateServices(business.getServices());
     57                    saveOrUpdateBusiness(business);
    5958                });
    6059    }
     
    6665    }
    6766
    68     public List<Business> findByBusinessTypeAndActiveStatus(BusinessType businessType) {
    69         return businessRepository.findBusinessesByBusinessTypeAndBusinessStatus(businessType, ACTIVE);
     67    public List<Business> findByBusinessTypeAndActiveStatus(Long businessTypeId) {
     68        return businessRepository.findBusinessesByBusinessStatusAndBusinessType_Id(ACTIVE, businessTypeId);
     69    }
     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        }
    7091    }
    7192}
  • src/main/java/edu/gjoko/schedlr/services/NomenclaturesService.java

    • Property mode changed from 100644 to 100755
  • src/main/java/edu/gjoko/schedlr/services/PostgresUserDetailsService.java

    • Property mode changed from 100644 to 100755
  • src/main/java/edu/gjoko/schedlr/services/StakeholderService.java

    • Property mode changed from 100644 to 100755
    r950fa0d r77205be  
    66import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
    77import org.springframework.stereotype.Service;
     8
     9import java.util.Optional;
    810
    911@Service
     
    2830        return stakeholderRepository.findById(id).get();
    2931    }
     32
     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        }
     43    }
    3044}
Note: See TracChangeset for help on using the changeset viewer.