diff --git a/src/main/java/com/github/prominence/carrepair/CarRepairApplication.java b/src/main/java/com/github/prominence/carrepair/CarRepairApplication.java index 52296f1..9640902 100644 --- a/src/main/java/com/github/prominence/carrepair/CarRepairApplication.java +++ b/src/main/java/com/github/prominence/carrepair/CarRepairApplication.java @@ -16,4 +16,7 @@ public class CarRepairApplication { } // TODO: logging -// TODO: DTO \ No newline at end of file +// TODO: DTO +// TODO: i18n +// TODO: tests +// TODO: big data \ No newline at end of file diff --git a/src/main/java/com/github/prominence/carrepair/controller/MechanicController.java b/src/main/java/com/github/prominence/carrepair/controller/MechanicController.java new file mode 100644 index 0000000..b129f68 --- /dev/null +++ b/src/main/java/com/github/prominence/carrepair/controller/MechanicController.java @@ -0,0 +1,98 @@ +package com.github.prominence.carrepair.controller; + +import com.github.prominence.carrepair.enums.OrderStatus; +import com.github.prominence.carrepair.model.Mechanic; +import com.github.prominence.carrepair.service.MechanicService; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.ui.ModelMap; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.annotation.*; + +import javax.validation.Valid; +import java.util.Map; +import java.util.Optional; + +@Controller +@RequestMapping("/mechanic") +public class MechanicController { + + private MechanicService mechanicService; + + public MechanicController(MechanicService mechanicService) { + this.mechanicService = mechanicService; + } + + @GetMapping + public String index(ModelMap modelMap) { + Page clientList = mechanicService.findAll((Pageable) modelMap.get("pagination")); + + modelMap.addAttribute("mechanicList", clientList.getContent()); + modelMap.addAttribute("totalPages", clientList.getTotalPages()); + + return "mechanic/index"; + } + + @GetMapping(value = "/create") + public String create(Model model) { + model.addAttribute("mechanic", new Mechanic()); + + return "mechanic/edit"; + } + + @GetMapping(value = "/edit/{id}") + public String edit(@PathVariable Long id, Model model) { + Optional clientOptional = mechanicService.findById(id); + if (clientOptional.isPresent()) { + model.addAttribute("mechanic", clientOptional.get()); + return "mechanic/edit"; + } else { + // TODO: need to show warning + return "redirect:/mechanic"; + } + } + + @PostMapping(value = "/update/{id}") + public String update(@Valid Mechanic client, BindingResult bindingResult, @PathVariable long id) { + if (bindingResult.hasErrors()) { + client.setId(id); // why should we do this? + return "mechanic/edit"; + } + + mechanicService.save(client); + return "redirect:/mechanic"; + } + + @PostMapping(value = "/create") + public String save(@Valid Mechanic client, BindingResult bindingResult) { + if (bindingResult.hasErrors()) { + return "mechanic/edit"; + } + + mechanicService.save(client); + return "redirect:/mechanic"; + } + + @DeleteMapping(value = "/delete/{id}") + public ResponseEntity delete(@PathVariable Long id) { + boolean deleteSuccess = mechanicService.deleteMechanicById(id); + + if (deleteSuccess) { + return ResponseEntity.ok().build(); + } else { + return ResponseEntity.notFound().build(); + } + } + + @GetMapping(value = "/statistics/{id}") + public String statistics(@PathVariable Long id, Model model) { + Map mechanicStatistics = mechanicService.getOrderStatistics(id); + model.addAttribute("statistics", mechanicStatistics); + + return "mechanic/statistics"; + } + +} diff --git a/src/main/java/com/github/prominence/carrepair/demo/DemoDataPopulator.java b/src/main/java/com/github/prominence/carrepair/demo/DemoDataPopulator.java index 21febfe..9e1d929 100644 --- a/src/main/java/com/github/prominence/carrepair/demo/DemoDataPopulator.java +++ b/src/main/java/com/github/prominence/carrepair/demo/DemoDataPopulator.java @@ -2,11 +2,14 @@ package com.github.prominence.carrepair.demo; import com.github.javafaker.Faker; import com.github.prominence.carrepair.model.Client; -import com.github.prominence.carrepair.repository.ClientRepository; +import com.github.prominence.carrepair.model.Mechanic; +import com.github.prominence.carrepair.service.ClientService; +import com.github.prominence.carrepair.service.MechanicService; import org.springframework.boot.CommandLineRunner; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; +import java.math.BigDecimal; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -15,10 +18,10 @@ import java.util.stream.Stream; public class DemoDataPopulator { private static final int COUNT = 10; - @Bean - public CommandLineRunner demoData(ClientRepository clientRepository) { - Faker faker = new Faker(); + private Faker faker = new Faker(); + @Bean + public CommandLineRunner clientDemoData(ClientService clientService) { List demoClientList = Stream.generate(() -> { Client client = new Client(); client.setFirstName(faker.name().firstName()); @@ -30,7 +33,24 @@ public class DemoDataPopulator { }).limit(COUNT).collect(Collectors.toList()); return args -> { - demoClientList.forEach(clientRepository::save); + demoClientList.forEach(clientService::save); + }; + } + + @Bean + public CommandLineRunner mechanicDemoData(MechanicService mechanicService) { + List demoMechanicList = Stream.generate(() -> { + Mechanic mechanic = new Mechanic(); + mechanic.setFirstName(faker.name().firstName()); + mechanic.setLastName(faker.name().lastName()); + mechanic.setMiddleName(faker.name().username()); + mechanic.setHourlyPayment(BigDecimal.valueOf(faker.number().randomDouble(3, 100, 999))); + System.out.println(mechanic); // demo output + return mechanic; + }).limit(COUNT).collect(Collectors.toList()); + + return args -> { + demoMechanicList.forEach(mechanicService::save); }; } diff --git a/src/main/java/com/github/prominence/carrepair/model/Mechanic.java b/src/main/java/com/github/prominence/carrepair/model/Mechanic.java index 531fd44..61a904f 100644 --- a/src/main/java/com/github/prominence/carrepair/model/Mechanic.java +++ b/src/main/java/com/github/prominence/carrepair/model/Mechanic.java @@ -3,14 +3,17 @@ package com.github.prominence.carrepair.model; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Table; +import javax.validation.constraints.Min; import javax.validation.constraints.NotNull; import java.math.BigDecimal; +import java.util.Objects; @Entity @Table(name = "mechanic") public class Mechanic extends Person { @NotNull + @Min(value = 0) @Column(name = "hourlyPayment") private BigDecimal hourlyPayment; @@ -29,4 +32,29 @@ public class Mechanic extends Person { public void setHourlyPayment(BigDecimal hourlyPayment) { this.hourlyPayment = hourlyPayment; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Mechanic)) return false; + if (!super.equals(o)) return false; + Mechanic mechanic = (Mechanic) o; + return Objects.equals(hourlyPayment, mechanic.hourlyPayment); + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), hourlyPayment); + } + + @Override + public String toString() { + return "Mechanic{" + + "hourlyPayment=" + hourlyPayment + + ", id=" + id + + ", firstName='" + firstName + '\'' + + ", middleName='" + middleName + '\'' + + ", lastName='" + lastName + '\'' + + '}'; + } } diff --git a/src/main/java/com/github/prominence/carrepair/repository/OrderRepository.java b/src/main/java/com/github/prominence/carrepair/repository/OrderRepository.java index e56d1a6..49c0d95 100644 --- a/src/main/java/com/github/prominence/carrepair/repository/OrderRepository.java +++ b/src/main/java/com/github/prominence/carrepair/repository/OrderRepository.java @@ -4,6 +4,10 @@ import com.github.prominence.carrepair.model.Order; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; +import java.util.List; + @Repository public interface OrderRepository extends JpaRepository { + + public List findAllByMechanic_Id(Long id); } diff --git a/src/main/java/com/github/prominence/carrepair/service/MechanicService.java b/src/main/java/com/github/prominence/carrepair/service/MechanicService.java new file mode 100644 index 0000000..ab5caf9 --- /dev/null +++ b/src/main/java/com/github/prominence/carrepair/service/MechanicService.java @@ -0,0 +1,61 @@ +package com.github.prominence.carrepair.service; + +import com.github.prominence.carrepair.enums.OrderStatus; +import com.github.prominence.carrepair.model.Mechanic; +import com.github.prominence.carrepair.model.Order; +import com.github.prominence.carrepair.repository.MechanicRepository; +import com.github.prominence.carrepair.repository.OrderRepository; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.stereotype.Service; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; + +@Service +public class MechanicService { + + private MechanicRepository mechanicRepository; + private OrderRepository orderRepository; + + public MechanicService(MechanicRepository mechanicRepository, OrderRepository orderRepository) { + this.mechanicRepository = mechanicRepository; + this.orderRepository = orderRepository; + } + + public Page findAll(Pageable pageable) { + return mechanicRepository.findAll(pageable); + } + + public Optional findById(Long id) { + return mechanicRepository.findById(id); + } + + public Mechanic save(Mechanic client) { + return mechanicRepository.save(client); + } + + public boolean deleteMechanicById(Long id) { + try { + mechanicRepository.deleteById(id); + return true; + } catch (Exception e) { + return false; + } + } + + public Map getOrderStatistics(Long mechanicId) { + Map statistics = new HashMap<>(); + statistics.put(OrderStatus.SCHEDULED, 0); + statistics.put(OrderStatus.ACCEPTED, 0); + statistics.put(OrderStatus.DONE, 0); + + List mechanicOrders = orderRepository.findAllByMechanic_Id(mechanicId); + + mechanicOrders.forEach(order -> statistics.merge(order.getOrderStatus(), 1, Integer::sum)); + + return statistics; + } +} diff --git a/src/main/resources/messages.properties b/src/main/resources/messages.properties index 571338a..b629f23 100644 --- a/src/main/resources/messages.properties +++ b/src/main/resources/messages.properties @@ -6,4 +6,25 @@ badge.orders = Orders home.requirements.label = Requirements -client.title = Clients \ No newline at end of file +client.title = Clients +mechanic.title = Mechanics + +firstName.label = First Name +middleName.label = Middle Name +lastName.label = Last Name +phoneNo.label = Phone No +hourlyPayment.label = Hourly Payment +status.label = Status +amount.label = Amount + +showStatistics.button = Statistics + +status.scheduled.label = Scheduled +status.done.label = Done +status.accepted.label = Accepted by client + +common.create.button = Create +common.delete.button = Delete +common.save.button = Save +common.edit.button = Edit +common.back.button = Back diff --git a/src/main/resources/messages_en.properties b/src/main/resources/messages_en.properties index 571338a..b629f23 100644 --- a/src/main/resources/messages_en.properties +++ b/src/main/resources/messages_en.properties @@ -6,4 +6,25 @@ badge.orders = Orders home.requirements.label = Requirements -client.title = Clients \ No newline at end of file +client.title = Clients +mechanic.title = Mechanics + +firstName.label = First Name +middleName.label = Middle Name +lastName.label = Last Name +phoneNo.label = Phone No +hourlyPayment.label = Hourly Payment +status.label = Status +amount.label = Amount + +showStatistics.button = Statistics + +status.scheduled.label = Scheduled +status.done.label = Done +status.accepted.label = Accepted by client + +common.create.button = Create +common.delete.button = Delete +common.save.button = Save +common.edit.button = Edit +common.back.button = Back diff --git a/src/main/resources/messages_ru.properties b/src/main/resources/messages_ru.properties index 2c12986..153a256 100644 --- a/src/main/resources/messages_ru.properties +++ b/src/main/resources/messages_ru.properties @@ -6,4 +6,25 @@ badge.orders = Заказы home.requirements.label = Требования -client.title = Клиенты \ No newline at end of file +client.title = Клиенты +mechanic.title = Механики + +firstName.label = Имя +middleName.label = Отчество +lastName.label = Фамилия +phoneNo.label = Номер телефона +hourlyPayment.label = Почасовая оплата +status.label = Статус +amount.label = Количество + +showStatistics.button = Статистика + +status.scheduled.label = Запланировано +status.done.label = Выполнено +status.accepted.label = Принято клиентом + +common.create.button = Создать +common.delete.button = Удалить +common.save.button = Сохранить +common.edit.button = Редактировать +common.back.button = Назад diff --git a/src/main/resources/templates/client/edit.html b/src/main/resources/templates/client/edit.html index 7a32bf9..38b92fd 100644 --- a/src/main/resources/templates/client/edit.html +++ b/src/main/resources/templates/client/edit.html @@ -10,28 +10,28 @@
- +
- +
- +
- +
@@ -39,8 +39,8 @@
- Back - + +
diff --git a/src/main/resources/templates/client/index.html b/src/main/resources/templates/client/index.html index 2c61b0f..0fc9589 100644 --- a/src/main/resources/templates/client/index.html +++ b/src/main/resources/templates/client/index.html @@ -26,16 +26,16 @@

- Create +
- - - - + + + + @@ -47,8 +47,8 @@ diff --git a/src/main/resources/templates/main.html b/src/main/resources/templates/main.html index 60494c1..1dd389d 100644 --- a/src/main/resources/templates/main.html +++ b/src/main/resources/templates/main.html @@ -20,7 +20,7 @@ diff --git a/src/main/resources/templates/mechanic/edit.html b/src/main/resources/templates/mechanic/edit.html new file mode 100644 index 0000000..659ebf6 --- /dev/null +++ b/src/main/resources/templates/mechanic/edit.html @@ -0,0 +1,50 @@ + + + + + + +
+

+ +
+
+ +
+ +
+
+
+
+ +
+ +
+
+
+
+ +
+ +
+
+
+
+ +
+ +
+
+
+
+
+ + +
+
+ +
+ + + \ No newline at end of file diff --git a/src/main/resources/templates/mechanic/index.html b/src/main/resources/templates/mechanic/index.html new file mode 100644 index 0000000..4bfcfa4 --- /dev/null +++ b/src/main/resources/templates/mechanic/index.html @@ -0,0 +1,62 @@ + + + + + + + + +
+

+ +
+ +
+ +
First NameMiddle NameLast NamePhone No  
- Edit - + +
+ + + + + + + + + + + + + + + + + + +
 
+
+ + + +
+
+
+
+ + + \ No newline at end of file diff --git a/src/main/resources/templates/mechanic/statistics.html b/src/main/resources/templates/mechanic/statistics.html new file mode 100644 index 0000000..82706ca --- /dev/null +++ b/src/main/resources/templates/mechanic/statistics.html @@ -0,0 +1,41 @@ + + + + + + +
+

+ + + + + + + + + + + + + + + + + + + + + + +
+
+
+ +
+
+
+ + + \ No newline at end of file