Changeset 1413ee2


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

Add all bug fixes.

Location:
src/main
Files:
4 added
15 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}
  • src/main/resources/data.sql

    r77205be r1413ee2  
    1919values (nextval('hibernate_sequence'), current_timestamp, '075658123', 'admin@schedlr.com', 'admin', 'admin', current_timestamp, '$2a$10$DJyjt.Vj/U8MEuYX1PXL9ukQSMwXHVdhre3POlTqpYzNxHB5gu/MW','ADMIN', 'admin'),
    2020        (nextval('hibernate_sequence'), current_timestamp, '071658125', 'user1@schedlr.com', 'gjoko', 'kostadinov', current_timestamp, '$2a$10$Zc28AcCpAgxB.e67UMF/2.FgchjH9QWB7z8nP0TdkrFneV4IHPXji','CUSTOMER', 'user'),
    21         (11111, current_timestamp, '078658523',  'c@c.com', 'Kliment', 'Nedelkovski', current_timestamp, '$2a$10$OratLSMKNsqd7Re.Md3I4.jrRHtTQNaLz/wUKo.I98..GufnD48uG','CUSTOMER', 'kliment123'),
    22         (10000, current_timestamp, '071218123', 'q@c.com', 'Stefan', 'Petrovski', current_timestamp, '$2a$10$x10O8eLp1r1wHwxDGvyo5.TBL216p8h797JJjQYEfFTbj/1bnCMki','BUSINESS_OWNER', 'stefan123');
     21        (11111, current_timestamp, '078658523',  'c@c.com', 'Kliment', 'Nedelkovski', current_timestamp, '$2a$10$OratLSMKNsqd7Re.Md3I4.jrRHtTQNaLz/wUKo.I98..GufnD48uG','CUSTOMER', 'c'),
     22        (10000, current_timestamp, '071218123', 'q@c.com', 'Stefan', 'Petrovski', current_timestamp, '$2a$10$x10O8eLp1r1wHwxDGvyo5.TBL216p8h797JJjQYEfFTbj/1bnCMki','BUSINESS_OWNER', 'q');
    2323
    2424insert into business (id, business_status, company_name, created, modified, business_type_id, owner_id)
    2525values (10001, 'ACTIVE', 'krojach stefan', current_timestamp, current_timestamp, 1, 10000);
    2626
    27 insert into service (id, created, modified, duration, price, cumulated_rating, reviews_count, business_id, service_type_id)
    28 values (10002, current_timestamp, current_timestamp, 60, 400, 0, 0, 10001, 6),
    29        (10003, current_timestamp, current_timestamp, 30, 200, 0, 0, 10001, 7);
     27insert into service (id, created, modified, service_status, duration, price, cumulated_rating, reviews_count, business_id, service_type_id)
     28values (10002, current_timestamp, current_timestamp, 'ACTIVE', 60, 400, 0, 0, 10001, 6),
     29       (10003, current_timestamp, current_timestamp, 'ACTIVE', 30, 200, 0, 0, 10001, 7);
    3030
    3131insert into appointment (id, created, modified, start_time, end_time, stakeholder_id, service_id)
  • src/main/resources/static/css/customer_admin.css

    r77205be r1413ee2  
    11.rate{
    2 
    32    border-bottom-right-radius: 12px;
    43    border-bottom-left-radius: 12px;
    5 
    64}
    75
  • src/main/resources/static/js/admin.js

    r77205be r1413ee2  
    2020            }
    2121        });
    22         console.log(JSON.stringify(companiesToSave));
    2322
    2423
    2524        $.ajax({
    26             url: "http://localhost:8080/api/business",
     25            url: "http://localhost:8080/api/admin",
    2726            type:"PATCH",
    2827            data: JSON.stringify(companiesToSave),
  • src/main/resources/static/js/business_admin.js

    r77205be r1413ee2  
    22    var business = {};
    33
    4     getBusinessInfo(business);
     4    getBusinessInfo().then(function (data) {
     5        business = data;
     6    });
    57
    68    getAppointments();
     
    3335    $("#update_services_button").click(function () {
    3436        businesses = [];
    35         console.log("Gjoko");
    3637        servicesObj = [];
    3738        $.each($('#predefined_services_admin_panel input:checked').siblings(), function (index, label) {
     
    4344            var price = $($($(label).parent()).siblings()[1]).children()[0].value;
    4445
    45             var serviceType = {}
     46            var serviceType = {};
    4647            if (parseInt(id) != -1) {
    4748                service['id'] = parseInt(id);
     
    5657            servicesObj.push(service);
    5758        });
    58         business['services'] = servicesObj;
    59 
    60         businesses.push(business);
    61         console.log(JSON.stringify(businesses));
    62 
    63         $.ajax({
    64             url: "http://localhost:8080/api/business",
    65             type:"PATCH",
    66             data: JSON.stringify(businesses),
    67             contentType:"application/json; charset=utf-8",
    68             dataType: 'text',
    69             success: function(succ){
    70                 alert( "Updates applied successfully" );
    71             },
    72             error: function(err) {
    73                 alert(err);
    74             }
    75         });
     59
     60        var servicesForDelete=[];
     61        $.each($('#predefined_services_admin_panel input:not(:checked)').siblings(), function (index, label) {
     62            let id = $(label).prop('for');
     63            let foundService = business['services'].find(obj => obj.id === parseInt(id));
     64            if (foundService !== undefined && foundService['serviceStatus'] === 'ACTIVE') {
     65                let service1 = {};
     66                service1['id'] = id;
     67                servicesForDelete.push(service1);
     68            }
     69        });
     70
     71
     72        updateServices(servicesObj).then(function (response) {
     73            getBusinessInfo().then(function (data) {
     74                business = data;
     75            });
     76        });
     77
     78        if (servicesForDelete.length > 0) {
     79            deleteServices(servicesForDelete).then(function (response) {
     80                getBusinessInfo().then(function (data) {
     81                    business = data;
     82                });
     83            });
     84        }
     85
    7686        event.preventDefault();
    7787    });
     
    7989    $("#update_owner_button").click(function() {
    8090        businesses = [];
    81         business['owner']['firstName'] = $('#firstName').val();
    82         business['owner']['lastName'] = $('#lastName').val();
    83         business['owner']['email'] = $('#email').val();
    84         business['owner']['phoneNumber'] = $('#phoneNumber').val();
    85         business['owner']['username'] = $('#username').val();
     91        owner={};
     92        owner['firstName'] = $('#firstName').val();
     93        owner['lastName'] = $('#lastName').val();
     94        owner['email'] = $('#email').val();
     95        owner['phoneNumber'] = $('#phoneNumber').val();
     96        owner['username'] = $('#username').val();
     97
     98        business['owner'] = owner;
    8699
    87100        businesses.push(business);
    88         console.log(JSON.stringify(businesses));
    89 
    90         $.ajax({
    91             url: "http://localhost:8080/api/business",
    92             type:"PATCH",
    93             data: JSON.stringify(businesses),
    94             contentType:"application/json; charset=utf-8",
    95             dataType: 'text',
    96             success: function(succ){
    97                 alert( "Updates applied successfully" );
    98             },
    99             error: function(err) {
    100                 alert(err);
    101             }
     101
     102        updateBusinessInfo(businesses).then(function() {
     103            getBusinessInfo().then(function (found) {
     104                business = found;
     105            });
    102106        });
    103107    });
    104108    event.preventDefault();
    105109});
     110
     111function updateBusinessInfo(businessList) {
     112    return $.ajax({
     113        url: "http://localhost:8080/api/business",
     114        type:"PATCH",
     115        data: JSON.stringify(businessList),
     116        contentType:"application/json; charset=utf-8",
     117        dataType: 'text',
     118        success: function(succ){
     119            alert( "Updates applied successfully" );
     120        },
     121        error: function(err) {
     122            alert(err);
     123        }
     124    });
     125}
     126
     127function updateServices(serviceList) {
     128    return $.ajax({
     129        url: "http://localhost:8080/api/service",
     130        type:"PATCH",
     131        data: JSON.stringify(serviceList),
     132        contentType:"application/json; charset=utf-8",
     133        dataType: 'text',
     134        success: function(succ){
     135            alert( "Updates applied successfully" );
     136        },
     137        error: function(err) {
     138            alert(err);
     139        }
     140    });
     141}
     142
     143function deleteServices(services) {
     144    return $.ajax({
     145        url: "http://localhost:8080/api/service/delete",
     146        type:"POST",
     147        data: JSON.stringify(services),
     148        contentType:"application/json; charset=utf-8",
     149        dataType: 'text',
     150        success: function(succ){
     151            console.log("services deleted successfully");
     152        },
     153        error: function(err) {
     154            console.error(err);
     155        }
     156    });
     157}
    106158
    107159function cancelAppointment(appointmentId) {
     
    123175        url: "http://localhost:8080/api/appointment/future/me"
    124176    }).then(function (data) {
    125         console.log(data);
    126177        var $el = $("#bookings-table-body");
    127178        $el.empty();
     
    154205}
    155206
    156 function getBusinessInfo(business) {
    157     $.ajax({
     207function getBusinessInfo() {
     208    return $.ajax({
    158209        url: "http://localhost:8080/api/business/me"
    159     }).then(function (data) {
    160         business = data;
     210    }).then(function (business) {
    161211        var $header = $("#header");
    162212
    163213        // header
    164         $header.text($header.text() + " " + business["owner"]['firstName'] + " " + business['owner']['lastName']);
     214        $header.text("Welcome back " + business["owner"]['firstName'] + " " + business['owner']['lastName']);
    165215
    166216        // business info
     
    181231        var $el = $("#predefined_services_admin_panel");
    182232        $el.empty();
     233
    183234        $.each(business['services'], function (index, obj) {
    184             $el.append(
    185                 '<div class=\"form-outline mb-4\">' +
     235
     236            var element = '<div class=\"form-outline mb-4\">' +
    186237                '    <div class="row">' +
    187238                '        <div class="col-md-6">\n' +
    188                 '            <input class="form-check-input" type="checkbox" checked value=\"' + obj.id + '\" id=\"' + obj.id + '\">\n' +
     239                '            <input class="form-check-input" type="checkbox" ';
     240
     241            if (obj['serviceStatus'] === 'ACTIVE') {
     242                element += 'checked';
     243            }
     244
     245            element += ' value=\"' + obj.id + '\" id=\"' + obj.id + '\">\n' +
    189246                '            <label class="form-check-label" for=\"' + obj.id + '\">\n' + obj['serviceType'].name + '</label>\n' +
    190247                '        </div>' +
     
    196253                '        </div>' +
    197254                '    </div>' +
    198                 '</div>');
    199         });
    200     });
    201 }
     255                '</div>';
     256
     257            $el.append(element);
     258        });
     259        return business;
     260    });
     261}
  • src/main/resources/static/js/customer_admin.js

    r77205be r1413ee2  
    1616        customerForSave['phoneNumber'] = $('#phoneNumber').val();
    1717        customerForSave['username'] = $('#username').val();
    18 
    19         console.log(JSON.stringify(customerForSave));
    2018
    2119        $.ajax({
     
    5553        const recipientId = button.getAttribute('data-bs-id');
    5654
    57         console.log(recipientName + ' ' + recipientId);
    58         // If necessary, you could initiate an Ajax request here
    59         // and then do the updating in a callback.
    60 
    6155        // Update the modal's content.
    6256        const modalTitle = reviewModal.querySelector('.modal-title');
     
    9791        url: "http://localhost:8080/api/appointment/future/me"
    9892    }).then(function (data) {
    99         console.log(data);
    10093        var $el = $("#appointments-table-body");
    10194        $el.empty();
     
    194187        url: "http://localhost:8080/api/review/me"
    195188    }).then(function (data) {
    196         console.log(data);
    197189        var $el = $("#my-reviews");
    198190        $el.empty();
     
    233225            $('#reviewModal').modal('toggle');
    234226            getPastAppointments();
     227            getReviewsForCustomer();
    235228        },
    236229        error: function(error) {
     
    248241        }).success(function (data) {
    249242            alert("Appointment successfully canceled.")
    250             getAppointments();
     243            getFutureAppointments();
    251244        }).error(function (error) {
    252245            alert("Something went wrong.");
  • src/main/resources/static/js/homepage.js

    r77205be r1413ee2  
    102102            // get already stored service from in-memory
    103103            selectedServices = businesses.find(item => item.id == selectedVal)['services'];
     104            console.log(selectedServices);
    104105            resetAndAppendServices(selectedServices);
    105106        });
     
    164165
    165166    $.each(services, function (index, obj) {
    166         $el.append("<option value=" + obj.id + ">" + obj.serviceType.name + "</option>");
     167        if (obj.serviceStatus === 'ACTIVE') {
     168            $el.append("<option value=" + obj.id + ">" + obj.serviceType.name + "</option>");
     169        }
    167170    });
    168171}
Note: See TracChangeset for help on using the changeset viewer.