Changeset 9050790


Ignore:
Timestamp:
03/13/23 23:30:41 (21 months ago)
Author:
Gjoko Kostadinov <gjoko.kostadinov@…>
Branches:
master
Children:
8bcd64c
Parents:
46fd0c7
Message:

Add admin functionality for activating or deactivating companies

Location:
src/main
Files:
1 added
7 edited

Legend:

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

    r46fd0c7 r9050790  
    2424        HttpSession session = httpServletRequest.getSession(false);
    2525
    26         if(httpServletRequest.getRequestURI().endsWith(".js") || httpServletRequest.getRequestURI().endsWith(".css")) {
     26        if(httpServletRequest.getRequestURI().endsWith(".js")
     27                || httpServletRequest.getRequestURI().endsWith(".css")
     28                || httpServletRequest.getRequestURI().startsWith("/api")) {
    2729            filterChain.doFilter(servletRequest, servletResponse);
    2830            return;
  • src/main/java/edu/gjoko/schedlr/controllers/rest/BusinessController.java

    r46fd0c7 r9050790  
    2121
    2222    @GetMapping
    23     public List<Business> getNewBusinesses() {
    24         return businessService.getNewBusiness();
     23    public List<Business> findAll() {
     24        return businessService.findAll();
    2525    }
    2626
    2727    @PatchMapping
    28     public void activateBusinesses(@RequestBody List<Business> businessList) {
    29         businessService.activateBusinesses(businessList);
     28    public void updateBusinesses(@RequestBody List<Business> businessList) {
     29        businessService.updateBusinesses(businessList);
    3030    }
    3131}
  • src/main/java/edu/gjoko/schedlr/entity/BusinessStatus.java

    r46fd0c7 r9050790  
    44    NEW,
    55    ACTIVE,
    6     BLOCKED
     6    DEACTIVATED
    77}
  • src/main/java/edu/gjoko/schedlr/services/BusinessService.java

    r46fd0c7 r9050790  
    22
    33import edu.gjoko.schedlr.entity.Business;
    4 import edu.gjoko.schedlr.entity.BusinessStatus;
    54import edu.gjoko.schedlr.entity.ServiceType;
    65import edu.gjoko.schedlr.entity.StakeholderType;
     
    1110
    1211import java.util.List;
    13 import java.util.stream.Collectors;
    1412
    1513import static edu.gjoko.schedlr.entity.BusinessStatus.ACTIVE;
     
    3230    public void saveNewServiceTypes(Business business) {
    3331        business.getServices()
    34             .stream()
    35             .forEach(service -> {
    36                 if(service.getServiceType().getId() == null) {
    37                     ServiceType serviceType = service.getServiceType();
    38                     serviceType.setBusinessType(business.getBusinessType());
    39                     service.setServiceType(serviceTypeRepository.save(serviceType));
    40                 }
    41             });
     32                .stream()
     33                .forEach(service -> {
     34                    if (service.getServiceType().getId() == null) {
     35                        ServiceType serviceType = service.getServiceType();
     36                        serviceType.setBusinessType(business.getBusinessType());
     37                        service.setServiceType(serviceTypeRepository.save(serviceType));
     38                    }
     39                });
    4240    }
    4341
    44     public List<Business> getNewBusiness() {
    45         return businessRepository.findBusinessesByBusinessStatus(NEW);
     42    public List<Business> findAll() {
     43        return businessRepository.findAll();
    4644    }
    4745
    48     public void activateBusinesses(List<Business> businessList) {
     46    public void updateBusinesses(List<Business> businessList) {
    4947        businessList
    5048                .stream()
    5149                .forEach(business -> {
    52                     business.setBusinessStatus(ACTIVE);
    5350                    businessRepository.save(business);
    5451                });
  • src/main/resources/static/js/admin.js

    r46fd0c7 r9050790  
    11$(document).ready(function() {
     2    var companies = {};
    23
     4    $.ajax({
     5       url: "http://localhost:8080/api/business"
     6    }).then(function (data) {
     7        companies = data;
     8        var $el = $("#table_body");
     9
     10        $.each(data, function (index, obj) {
     11            if(obj.businessStatus == "NEW" || obj.businessStatus == "DEACTIVATED") {
     12                $el.append("<tr>\n" +
     13                    "            <th scope=\"row\">" + obj.id + "</th>\n" +
     14                    "            <td>" + obj.companyName + "</td>\n" +
     15                    "            <td>" + obj.owner.firstName + " " + obj.owner.lastName + "</td>\n" +
     16                    "            <td><input class=\"form-check-input\" type=\"checkbox\" value=\"" + obj.id +"\"></td>\n" +
     17                    "        </tr>")
     18            }
     19            if (obj.businessStatus == "ACTIVE") {
     20                $el.append("<tr>\n" +
     21                    "            <th scope=\"row\">" + obj.id + "</th>\n" +
     22                    "            <td>" + obj.companyName + "</td>\n" +
     23                    "            <td>" + obj.owner.firstName + " " + obj.owner.lastName + "</td>\n" +
     24                    "            <td><input class=\"form-check-input\" type=\"checkbox\" checked value=\"" + obj.id +"\"></td>\n" +
     25                    "        </tr>")
     26            }
     27        });
     28    });
     29
     30    $('#save_button').click(function () {
     31        $.each($('#table_body tr'), function(index, row) {
     32            if(companies[index].businessStatus == "NEW" &&  $($($(row).children() [3]).children()[0]).is(':checked')) {
     33                companies[index]['businessStatus'] = 'ACTIVE';
     34            }
     35            if(companies[index].businessStatus == "ACTIVE" && ! $($($(row).children() [3]).children()[0]).is(':checked')) {
     36                companies[index]['businessStatus'] = 'DEACTIVATED';
     37            }
     38        });
     39        console.log(JSON.stringify(companies));
     40        $.ajax({
     41            url: "http://localhost:8080/api/business",
     42            type:"PATCH",
     43            data: JSON.stringify(companies),
     44            contentType:"application/json; charset=utf-8",
     45            dataType: 'text',
     46            success: function(succ){
     47                alert( "Updates applied successfully" );
     48            },
     49            error: function(err) {
     50                alert(err);
     51            }
     52        });
     53        event.preventDefault();
     54    });
    355});
  • src/main/resources/static/js/register_business.js

    r46fd0c7 r9050790  
    4242        // clear the input
    4343        $("#input_service").val('');
    44 
    45         /*$("#predefined_services").append(
    46             '<div class="form-check">\n' +
    47             '    <input class="form-check-input" type="checkbox" checked value=\"' + -1 + '\" id=\"'+ -1 +'\">\n' +
    48             '        <label class="form-check-label" for=\"' + -1 + '\">\n' +
    49             input_service +
    50             '         </label>\n' +
    51             '    </div>'
    52         );*/
    5344
    5445        $("#predefined_services").append(
  • src/main/resources/templates/admin.html

    r46fd0c7 r9050790  
    22<html lang="en">
    33<head>
    4   <title>Schedlr</title>
     4    <title>Schedlr</title>
    55
    6   <meta charset="utf-8"/>
    7   <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
     6    <meta charset="utf-8"/>
     7    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
    88
    9   <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet"
    10         integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
     9    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet"
     10          integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
     11    <link href="css/admin.css" rel="stylesheet"/>
    1112</head>
    1213<body>
    13 ADMIN
     14<!-- Navbar start -->
     15<header class="p-3 mb-3 border-bottom">
     16    <div class="container">
     17        <div class="d-flex flex-wrap align-items-center justify-content-center justify-content-lg-start">
     18            Welcome to the admin page
     19        </div>
     20    </div>
     21</header>
    1422
     23<div class="container">
    1524
     25    <h3>Current companies</h3>
     26    <!-- Navbar end -->
     27    <table class="table table-striped" id="new_table">
     28        <thead class="thead-dark">
     29        <tr>
     30            <th scope="col">Id</th>
     31            <th scope="col">Name</th>
     32            <th scope="col">Owner</th>
     33            <th scope="col">Active</th>
     34        </tr>
     35        </thead>
     36        <tbody id="table_body">
     37        </tbody>
     38    </table>
     39
     40    <!-- Submit button -->
     41    <button type="buttom" class="btn btn-primary btn-block mb-4" id="save_button">
     42        Save changes
     43    </button>
     44</div>
    1645<!-- jQuery library -->
    1746<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"
Note: See TracChangeset for help on using the changeset viewer.