Ignore:
Timestamp:
01/06/23 15:26:00 (23 months ago)
Author:
andrejtodorovski <82031894+andrejtodorovski@…>
Branches:
main
Children:
9dcbf44
Parents:
f484b14
Message:

Added full functionality for adding review

File:
1 edited

Legend:

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

    rf484b14 rd4b888e  
    11package com.example.autopartz.controller;
    22
    3 import com.example.autopartz.model.CarSample;
    4 import com.example.autopartz.model.Client;
    5 import com.example.autopartz.model.ServiceBook;
     3import com.example.autopartz.model.*;
     4import com.example.autopartz.model.views.RepairsForUser;
    65import com.example.autopartz.repository.*;
    76import com.example.autopartz.service.CarService;
     7import com.example.autopartz.service.RepairService;
    88import com.example.autopartz.service.UserService;
    99import org.springframework.stereotype.Controller;
    1010import org.springframework.ui.Model;
    11 import org.springframework.web.bind.annotation.GetMapping;
    12 import org.springframework.web.bind.annotation.PostMapping;
    13 import org.springframework.web.bind.annotation.RequestMapping;
    14 import org.springframework.web.bind.annotation.RequestParam;
     11import org.springframework.web.bind.annotation.*;
    1512
    1613import javax.servlet.http.HttpServletRequest;
    1714import javax.servlet.http.HttpServletResponse;
    1815import java.io.IOException;
     16import java.util.ArrayList;
     17import java.util.List;
    1918
    2019@Controller
     
    2827    private final CarSampleRepository carSampleRepository;
    2928    private final ServiceBookRepository serviceBookRepository;
    30     public UserController(OrdersForUserRepository ordersForUserRepository, RepairsForUserRepository repairsForUserRepository, ReviewsForUserRepository reviewsForUserRepository, UserService userService, CarService carService, CarSampleRepository carSampleRepository, ServiceBookRepository serviceBookRepository) {
     29    private final RepairService repairService;
     30    private final ReviewRepository reviewRepository;
     31    private final RepairRepository repairRepository;
     32    private final OrderRepository orderRepository;
     33
     34    public UserController(OrdersForUserRepository ordersForUserRepository, RepairsForUserRepository repairsForUserRepository, ReviewsForUserRepository reviewsForUserRepository, UserService userService, CarService carService, CarSampleRepository carSampleRepository, ServiceBookRepository serviceBookRepository, RepairService repairService, ReviewRepository reviewRepository,
     35                          RepairRepository repairRepository,
     36                          OrderRepository orderRepository) {
    3137        this.ordersForUserRepository = ordersForUserRepository;
    3238        this.repairsForUserRepository = repairsForUserRepository;
     
    3642        this.carSampleRepository = carSampleRepository;
    3743        this.serviceBookRepository = serviceBookRepository;
     44        this.repairService = repairService;
     45        this.reviewRepository = reviewRepository;
     46        this.repairRepository = repairRepository;
     47        this.orderRepository = orderRepository;
    3848    }
    3949    @GetMapping("orders")
     
    4858        Integer id = userService.findByUsername(request.getRemoteUser()).getId();
    4959        model.addAttribute("userRepairs",repairsForUserRepository.findAllByUserid(id));
     60        List<Boolean> hasReview = new ArrayList<>(100);
     61        for (int i = 0; i < orderRepository.findAll().size(); i++) {
     62            hasReview.add(false);
     63        }
     64        for(RepairsForUser rfu: repairsForUserRepository.findAll()){
     65            if(reviewRepository.findByRepair(repairService.findById(rfu.getRepairid()))==null){
     66                hasReview.set(rfu.getRepairid(),false);
     67            }
     68            else {
     69                hasReview.set(rfu.getRepairid(),true);
     70            }
     71        }
     72        model.addAttribute("hasReview", hasReview);
    5073        model.addAttribute("bodyContent","repairsForUser");
    5174        return "master-template";
     
    78101        }
    79102    }
     103    @GetMapping("/repairs/addReview/{id}")
     104    public String addReviewFor(@PathVariable Integer id, Model model){
     105        model.addAttribute("bodyContent","addReviewPage");
     106        model.addAttribute("repair", repairService.findById(id));
     107        return "master-template";
     108    }
     109    @PostMapping("/repairs/addReview/{id}")
     110    public void addReview(@PathVariable Integer id, @RequestParam Integer rating, @RequestParam(required = false) String comment,
     111                          Model model, HttpServletResponse response, HttpServletRequest request){
     112        Client client = (Client) userService.findByUsername(request.getRemoteUser());
     113        Repair repair = repairService.findById(id);
     114        reviewRepository.save(new Review(rating,
     115                comment==null ? "" : comment, repair,client));
     116        try {
     117            response.sendRedirect("/repairs");
     118        } catch (IOException e) {
     119            throw new RuntimeException(e);
     120        }
     121    }
    80122}
Note: See TracChangeset for help on using the changeset viewer.