diff --git a/src/main/java/com/example/autoservice/clients/api/ClientsController.java b/src/main/java/com/example/autoservice/clients/api/ClientsController.java index 593a6f0..60ee72f 100644 --- a/src/main/java/com/example/autoservice/clients/api/ClientsController.java +++ b/src/main/java/com/example/autoservice/clients/api/ClientsController.java @@ -5,8 +5,10 @@ import java.util.List; import org.modelmapper.ModelMapper; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; +import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; @@ -14,6 +16,7 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import com.example.autoservice.clients.service.ClientsService; +import com.example.autoservice.core.configuration.Constants; import com.example.autoservice.clients.model.ClientsEntity; import jakarta.validation.Valid; @@ -23,6 +26,8 @@ import jakarta.validation.Valid; public class ClientsController { public static final String URL = "/clients"; private static final String CLIENTS_VIEW = "clients"; + private static final String CLIENTS_ATTRIBUTE = "clients"; + private static final String CLIENTS_EDIT_VIEW = "client-edit"; private final ClientsService clientsService; private final ModelMapper modelMapper; @@ -38,7 +43,7 @@ public class ClientsController { private ClientsEntity toEntity(ClientsDto dto) { return modelMapper.map(dto, ClientsEntity.class); } - @GetMapping() + @GetMapping public String getAll(Model model){ List lines = clientsService.getAll().stream() .map(this::toDto) @@ -48,6 +53,51 @@ public class ClientsController { return CLIENTS_VIEW; } + @GetMapping("/edit/") + public String create(Model model){ + model.addAttribute(CLIENTS_ATTRIBUTE, new ClientsDto()); + return CLIENTS_EDIT_VIEW; + } + + @PostMapping("/edit/") + public String create( + @ModelAttribute(name = CLIENTS_ATTRIBUTE) @Valid ClientsDto client, + BindingResult bindingResult, + Model model) { + if (bindingResult.hasErrors()) { + return CLIENTS_EDIT_VIEW; + } + clientsService.create(toEntity(client)); + return Constants.REDIRECT_VIEW + URL; + } + + @GetMapping("/edit/{id}") + public String update( + @PathVariable(name = "id") Long id, + Model model) { + if (id <= 0) { + throw new IllegalArgumentException(); + } + model.addAttribute(CLIENTS_ATTRIBUTE, toDto(clientsService.get(id))); + return CLIENTS_EDIT_VIEW; + } + + @PostMapping("/edit/{id}") + public String update( + @PathVariable(name = "id") Long id, + @ModelAttribute(name = CLIENTS_ATTRIBUTE) @Valid ClientsDto client, + BindingResult bindingResult, + Model model) { + if (bindingResult.hasErrors()) { + return CLIENTS_EDIT_VIEW; + } + if (id <= 0) { + throw new IllegalArgumentException(); + } + clientsService.update(id, toEntity(client)); + return Constants.REDIRECT_VIEW + URL; + } + // @GetMapping // public List getAll() { // return clientsService.getAll().stream().map(this::toDto).toList(); diff --git a/src/main/java/com/example/autoservice/clients/api/ClientsDto.java b/src/main/java/com/example/autoservice/clients/api/ClientsDto.java index 57089ee..7591348 100644 --- a/src/main/java/com/example/autoservice/clients/api/ClientsDto.java +++ b/src/main/java/com/example/autoservice/clients/api/ClientsDto.java @@ -1,5 +1,6 @@ package com.example.autoservice.clients.api; +import java.time.ZoneId; import java.util.Date; import com.fasterxml.jackson.annotation.JsonProperty; @@ -14,7 +15,7 @@ public class ClientsDto{ @NotNull private String last_name; private String middle_name; - private Date date_birthday; + private String date_birthday; @NotNull private String phone_number; @@ -50,12 +51,13 @@ public class ClientsDto{ this.middle_name = middlename; } - public Date getDate_birthday(){ + public String getDate_birthday(){ return date_birthday; } public void setDate_birthday(Date datebirthday){ - this.date_birthday = datebirthday; + this.date_birthday = datebirthday.toInstant(). + atZone(ZoneId.systemDefault()).toLocalDate().toString(); } public String getPhone_number(){ diff --git a/src/main/java/com/example/autoservice/clients/model/ClientsEntity.java b/src/main/java/com/example/autoservice/clients/model/ClientsEntity.java index 0ba6039..e1e60ab 100644 --- a/src/main/java/com/example/autoservice/clients/model/ClientsEntity.java +++ b/src/main/java/com/example/autoservice/clients/model/ClientsEntity.java @@ -1,7 +1,8 @@ package com.example.autoservice.clients.model; -import com.example.autoservice.core.model.BaseEntity; import java.util.Date; + +import com.example.autoservice.core.model.BaseEntity; import jakarta.persistence.Column; import jakarta.persistence.Entity; import jakarta.persistence.Table; @@ -13,7 +14,7 @@ public class ClientsEntity extends BaseEntity{ private String first_name; @Column(nullable = false, unique = false, length = 25) private String last_name; - @Column(nullable = false, unique = false, length = 10) + @Column(nullable = true, unique = false, length = 10) private String middle_name; @Column(nullable = true, unique = false) private Date date_birthday; diff --git a/src/main/java/com/example/autoservice/clients/repository/ClientsRepository.java b/src/main/java/com/example/autoservice/clients/repository/ClientsRepository.java index e72456a..f1c434d 100644 --- a/src/main/java/com/example/autoservice/clients/repository/ClientsRepository.java +++ b/src/main/java/com/example/autoservice/clients/repository/ClientsRepository.java @@ -1,7 +1,5 @@ package com.example.autoservice.clients.repository; -import java.util.Optional; - import org.springframework.data.repository.CrudRepository; import com.example.autoservice.clients.model.ClientsEntity; diff --git a/src/main/java/com/example/autoservice/core/configuration/Constants.java b/src/main/java/com/example/autoservice/core/configuration/Constants.java index 17f6925..05c3b94 100644 --- a/src/main/java/com/example/autoservice/core/configuration/Constants.java +++ b/src/main/java/com/example/autoservice/core/configuration/Constants.java @@ -3,6 +3,8 @@ package com.example.autoservice.core.configuration; public class Constants { public static final String SEQUENCE_NAME = "hibernate_sequence"; + public static final String REDIRECT_VIEW = "redirect:"; + public static final String API_URL = "/api/1.0"; private Constants() { diff --git a/src/main/resources/templates/client-edit.html b/src/main/resources/templates/client-edit.html new file mode 100644 index 0000000..b5906f9 --- /dev/null +++ b/src/main/resources/templates/client-edit.html @@ -0,0 +1,52 @@ + + + + Редакторовать тип заказа + + + + + + + + +
+
+
+ + +
+
+ + +
+
+
+ + +
+
+
+ + +
+
+
+ + +
+
+
+ + +
+
+
+ + Отмена +
+
+
+ + diff --git a/src/main/resources/templates/clients.html b/src/main/resources/templates/clients.html index 6d56843..5f2c637 100644 --- a/src/main/resources/templates/clients.html +++ b/src/main/resources/templates/clients.html @@ -1,33 +1,48 @@ - + - Getting Started: Serving Web Content + Clients -
- +
+
+ + +
- + + + - - - - - - - + + + + + + + + +
ID First Name Last Name Middle NameDate of BirthDate of Birthday Phone Number
+
+ +
+
+
+ +
+
diff --git a/src/main/resources/templates/default.html b/src/main/resources/templates/default.html new file mode 100644 index 0000000..1082fbe --- /dev/null +++ b/src/main/resources/templates/default.html @@ -0,0 +1,40 @@ + + + + + + + autoservice + + + + + + + + +
+
+ + + + \ No newline at end of file