Changeset 9dcbf44 for src/main/java/com


Ignore:
Timestamp:
01/06/23 19:17:05 (23 months ago)
Author:
andrejtodorovski <82031894+andrejtodorovski@…>
Branches:
main
Children:
cab5859
Parents:
d4b888e
Message:

Added full functionality for changing delivery status,
added bootstrap for some pages

Location:
src/main/java/com/example/autopartz
Files:
3 added
4 edited

Legend:

Unmodified
Added
Removed
  • src/main/java/com/example/autopartz/controller/HomeController.java

    rd4b888e r9dcbf44  
    22
    33import com.example.autopartz.model.*;
     4import com.example.autopartz.model.DTO.OrderInfo;
     5import com.example.autopartz.model.manytomany.OrderContainsPart;
    46import com.example.autopartz.model.manytomany.PartIsInStockInWarehouse;
     7import com.example.autopartz.model.views.DeliveriesInProgress;
     8import com.example.autopartz.model.views.PartsForCarTypeAndCategory;
    59import com.example.autopartz.repository.*;
    610import com.example.autopartz.service.*;
    711import org.springframework.stereotype.Controller;
    812import org.springframework.ui.Model;
    9 import org.springframework.web.bind.annotation.GetMapping;
    10 import org.springframework.web.bind.annotation.PostMapping;
    11 import org.springframework.web.bind.annotation.RequestMapping;
    12 import org.springframework.web.bind.annotation.RequestParam;
     13import org.springframework.web.bind.annotation.*;
    1314
    1415import javax.servlet.http.HttpServletRequest;
     
    1617import javax.servlet.http.HttpSession;
    1718import java.io.IOException;
     19import java.util.ArrayList;
    1820import java.util.List;
    1921import java.util.Objects;
     
    3234    private final OrderService orderService;
    3335    private final UserService userService;
     36    private final DeliveriesInProgressRepository deliveriesInProgressRepository;
    3437    private final DeliveryService deliveryService;
    3538    private final PartIsInStockInWarehouseRepository partIsInStockInWarehouseRepository;
    3639    public HomeController(LoginService loginService, PartService partService, PartsForCarTypeAndCategoryRepository partsForCarTypeAndCategoryRepository, CarService carService, CategoryService categoryService, RepairShopReviewSummaryRepository repairShopReviewSummaryRepository, WarehouseRepository warehouseRepository,
    37                           OrderContainsPartRepository orderContainsPartRepository, OrderService orderService, UserService userService, DeliveryService deliveryService, PartIsInStockInWarehouseRepository partIsInStockInWarehouseRepository) {
     40                          OrderContainsPartRepository orderContainsPartRepository, OrderService orderService, UserService userService, DeliveriesInProgressRepository deliveriesInProgressRepository, DeliveryService deliveryService, PartIsInStockInWarehouseRepository partIsInStockInWarehouseRepository) {
    3841        this.loginService = loginService;
    3942        this.partService = partService;
     
    4649        this.orderService = orderService;
    4750        this.userService = userService;
     51        this.deliveriesInProgressRepository = deliveriesInProgressRepository;
    4852        this.deliveryService = deliveryService;
    4953        this.partIsInStockInWarehouseRepository = partIsInStockInWarehouseRepository;
     
    8791    @GetMapping("/filtered")
    8892    public String getPartsForCarTypeAndCategory(@RequestParam String cartype, @RequestParam String category, Model model){
    89         model.addAttribute("filtered", partsForCarTypeAndCategoryRepository.findAllByCartypeAndCategory(cartype,category));
     93        List<PartsForCarTypeAndCategory> tmp = partsForCarTypeAndCategoryRepository.findAllByCartypeAndCategory(cartype,category);
     94        if(tmp.size()==0){
     95            model.addAttribute("hasError",true);
     96            model.addAttribute("error","Не постојат такви производи, обидете се повторно");
     97        }
     98        else {
     99            model.addAttribute("hasError",false);
     100            model.addAttribute("filtered", tmp);
     101        }
    90102        model.addAttribute("bodyContent","filteredParts");
    91103        return "master-template";
     
    179191        return "master-template";
    180192    }
     193    @GetMapping("myNextDeliveries")
     194    public String myNextDeliveries(Model model, HttpServletRequest request){
     195        Deliveryman dm = (Deliveryman) userService.findByUsername(request.getRemoteUser());
     196        List<DeliveriesInProgress> ldip = deliveriesInProgressRepository.findAllByUserid(dm.getId());
     197        if(ldip.size()==0){
     198            model.addAttribute("hasError",true);
     199            model.addAttribute("error","Сите достави се завршени");
     200        }
     201        else {
     202            model.addAttribute("hasError",false);
     203            model.addAttribute("deliveries", deliveriesInProgressRepository.findAllByUserid(dm.getId()));
     204        }
     205        model.addAttribute("bodyContent","myNextDeliveries");
     206        return "master-template";
     207    }
     208    @PostMapping("/finishDelivery/{id}")
     209    public void finishDelivery(@PathVariable Integer id, Model model, HttpServletResponse response){
     210        Delivery d = deliveryService.findByOrder(orderService.findById(id));
     211        d.setStatus("finished");
     212        deliveryService.update(d);
     213        try {
     214            response.sendRedirect("/myDeliveries");
     215        } catch (IOException e) {
     216            throw new RuntimeException(e);
     217        }
     218    }
     219    @GetMapping("/order/{id}")
     220    public String getOrderInfo(@PathVariable Integer id, Model model){
     221        List<OrderContainsPart> list = orderContainsPartRepository.findAllByOrderid(id);
     222        List<OrderInfo> partList = new ArrayList<>();
     223        for (int i = 0; i < list.size(); i++) {
     224            OrderInfo oi = new OrderInfo(partService.findById(list.get(i).getPartid()).getName(),
     225                    list.get(i).getQuantity_order(),partService.findById(list.get(i).getPartid()).getManufacturer().getName());
     226            partList.add(oi);
     227        }
     228        model.addAttribute("parts",partList);
     229        model.addAttribute("o",orderService.findById(id));
     230        model.addAttribute("bodyContent","orderInfo");
     231        return "master-template";
     232    }
    181233}
  • src/main/java/com/example/autopartz/repository/DeliveryRepository.java

    rd4b888e r9dcbf44  
    33import com.example.autopartz.model.Delivery;
    44import com.example.autopartz.model.Deliveryman;
     5import com.example.autopartz.model.Order;
    56import org.springframework.data.jpa.repository.JpaRepository;
    67import org.springframework.stereotype.Repository;
     
    1112public interface DeliveryRepository extends JpaRepository<Delivery,Integer> {
    1213    List<Delivery> findAllByDeliveryman(Deliveryman deliveryman);
     14    List<Delivery> findAllByOrder(Order order);
    1315}
  • src/main/java/com/example/autopartz/service/DeliveryService.java

    rd4b888e r9dcbf44  
    33import com.example.autopartz.model.Delivery;
    44import com.example.autopartz.model.Deliveryman;
     5import com.example.autopartz.model.Order;
    56
    67import java.util.List;
     
    89public interface DeliveryService {
    910    List<Delivery> findAllByDeliverer(Deliveryman dm);
     11    Delivery findById(Integer id);
     12    void update(Delivery d);
     13    Delivery findByOrder(Order o);
    1014}
  • src/main/java/com/example/autopartz/service/impl/DeliveryServiceImpl.java

    rd4b888e r9dcbf44  
    33import com.example.autopartz.model.Delivery;
    44import com.example.autopartz.model.Deliveryman;
     5import com.example.autopartz.model.Order;
    56import com.example.autopartz.repository.DeliveryRepository;
    67import com.example.autopartz.service.DeliveryService;
     
    2122        return deliveryRepository.findAllByDeliveryman(dm);
    2223    }
     24
     25    @Override
     26    public Delivery findById(Integer id) {
     27        return deliveryRepository.findById(id).get();
     28    }
     29
     30    @Override
     31    public void update(Delivery d) {
     32        deliveryRepository.save(d);
     33    }
     34
     35    @Override
     36    public Delivery findByOrder(Order o) {
     37        return deliveryRepository.findAllByOrder(o).stream().findFirst().orElseThrow(RuntimeException::new);
     38    }
    2339}
Note: See TracChangeset for help on using the changeset viewer.