diff --git a/src/main/java/com/example/autoservice/AutoserviceApplication.java b/src/main/java/com/example/autoservice/AutoserviceApplication.java index 7f79b5c..e6557e8 100644 --- a/src/main/java/com/example/autoservice/AutoserviceApplication.java +++ b/src/main/java/com/example/autoservice/AutoserviceApplication.java @@ -1,10 +1,11 @@ package com.example.autoservice; +import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication -public class AutoserviceApplication { +public class AutoserviceApplication implements CommandLineRunner{ public static void main(String[] args) { SpringApplication.run(AutoserviceApplication.class, args); diff --git a/src/main/java/com/example/autoservice/clients/api/ClientsController.java b/src/main/java/com/example/autoservice/clients/api/ClientsController.java new file mode 100644 index 0000000..70bcecc --- /dev/null +++ b/src/main/java/com/example/autoservice/clients/api/ClientsController.java @@ -0,0 +1,64 @@ +package com.example.autoservice.clients.api; + +import java.util.List; + +import org.modelmapper.ModelMapper; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.example.demo.core.configuration.Constants; +import com.example.autoservice.clients.service.ClientsService; +import com.example.autoservice.clients.model.ClientsEntity; + +import jakarta.validation.Valid; + +@RestController +@RequestMapping(Constants.API_URL + "/clients") +public class ClientsController { + private final ClientsService clientsService; + private final ModelMapper modelMapper; + + public DepartmentController(ClientsService clientsService, ModelMapper modelMapper) { + this.clientsService = clientsService; + this.modelMapper = modelMapper; + } + + private ClientsDto toDto(ClientsEntity entity) { + return modelMapper.map(entity, ClientsDto.class); + } + + private ClientsEntity toEntity(ClientsDto dto) { + return modelMapper.map(dto, ClientsEntity.class); + } + + @GetMapping + public List getAll() { + return clientsService.getAll().stream().map(this::toDto).toList(); + } + + @GetMapping("/{id}") + public ClientsDto get(@PathVariable(name = "id") Long id) { + return toDto(clientsService.get(id)); + } + + @PostMapping + public ClientsDto create(@RequestBody @Valid ClientsDto dto) { + return toDto(clientsService.create(toEntity(dto))); + } + + @PutMapping("/{id}") + public ClientsDto update(@PathVariable(name = "id") Long id, @RequestBody ClientsDto dto) { + return toDto(clientsService.update(id, toEntity(dto))); + } + + @DeleteMapping("/{id}") + public ClientsDto delete(@PathVariable(name = "id") Long id) { + return toDto(clientsService.delete(id)); + } +} diff --git a/src/main/java/com/example/autoservice/clients/api/ClientsDto.java b/src/main/java/com/example/autoservice/clients/api/ClientsDto.java new file mode 100644 index 0000000..21bec1c --- /dev/null +++ b/src/main/java/com/example/autoservice/clients/api/ClientsDto.java @@ -0,0 +1,62 @@ +package com.example.autoservice.clients.api; + +public class ClientsDto{ + @JsonProperty(access = JsonProperty.Access.READ_ONLY) + private Long id; + @NotNull + private String first_name; + @NotNull + private String last_name; + private String middle_name; + private Date date_birthday; + @NotNull + private String phone_number; + + public Long getId(){ + return id; + } + + public void setId(Long id){ + this.id = id; + } + + public String getFirst_Name(){ + return first_name; + } + + public void setFirst_Name(String first_name){ + this.first_name = first_name; + } + + public String getLast_Name(){ + return last_name; + } + + public void setLast_Name(String last_name){ + this.last_name = last_name; + } + + public String getMiddle_Name(){ + return middle_name; + } + + public void setMiddle_Name(String middle_name){ + this.middle_name = middle_name; + } + + public Date getDate_Birthday(){ + return date_birthday; + } + + public void setDate_Birthday(Date date_birthday){ + this.date_birthday = date_birthday; + } + + public String getPhone_Number(){ + return phone_number; + } + + public void setPhone_Number(String phone_number){ + this.phone_number = phone_number; + } +} \ No newline at end of file diff --git a/src/main/java/com/example/autoservice/clients/model/ClientsEntity.java b/src/main/java/com/example/autoservice/clients/model/ClientsEntity.java new file mode 100644 index 0000000..0ba6039 --- /dev/null +++ b/src/main/java/com/example/autoservice/clients/model/ClientsEntity.java @@ -0,0 +1,74 @@ +package com.example.autoservice.clients.model; + +import com.example.autoservice.core.model.BaseEntity; +import java.util.Date; +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.Table; + +@Entity +@Table(name = "clients") +public class ClientsEntity extends BaseEntity{ + @Column(nullable = false, unique = true, length = 30) + private String first_name; + @Column(nullable = false, unique = false, length = 25) + private String last_name; + @Column(nullable = false, unique = false, length = 10) + private String middle_name; + @Column(nullable = true, unique = false) + private Date date_birthday; + @Column(nullable = false, unique = true, length = 12) + private String phone_number; + + public ClientsEntity(){ + + } + + public ClientsEntity(String first_name, String last_name, String middle_name, Date date_birthday, String phone_number){ + this.first_name = first_name; + this.last_name = last_name; + this.middle_name = middle_name; + this.date_birthday = date_birthday; + this.phone_number = phone_number; + } + + public String getFirst_Name(){ + return first_name; + } + + public void setFirst_Name(String first_name){ + this.first_name = first_name; + } + + public String getLast_Name(){ + return last_name; + } + + public void setLast_Name(String last_name){ + this.last_name = last_name; + } + + public String getMiddle_Name(){ + return middle_name; + } + + public void setMiddle_Name(String middle_name){ + this.middle_name = middle_name; + } + + public Date getDate_Birthday(){ + return date_birthday; + } + + public void setDate_Birthday(Date date_birthday){ + this.date_birthday = date_birthday; + } + + public String getPhone_Number(){ + return phone_number; + } + + public void setPhone_Number(String phone_number){ + this.phone_number = phone_number; + } +} diff --git a/src/main/java/com/example/autoservice/clients/repository/ClientsRepository.java b/src/main/java/com/example/autoservice/clients/repository/ClientsRepository.java new file mode 100644 index 0000000..35bed82 --- /dev/null +++ b/src/main/java/com/example/autoservice/clients/repository/ClientsRepository.java @@ -0,0 +1,10 @@ +package com.example.autoservice.clients.repository; + +import java.util.Optional; + +import org.springframework.data.repository.CrudRepository; + +import com.example.autoservice.clients.model.ClientEntity; + +public interface ClientsRepository extends CrudRepository { +} diff --git a/src/main/java/com/example/autoservice/clients/service/ClientsService.java b/src/main/java/com/example/autoservice/clients/service/ClientsService.java new file mode 100644 index 0000000..6a29be3 --- /dev/null +++ b/src/main/java/com/example/autoservice/clients/service/ClientsService.java @@ -0,0 +1,57 @@ +package com.example.autoservice.clients.service; + +import java.util.List; +import java.util.stream.StreamSupport; + +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import com.example.autoservice.clients.model.ClientsEntity; +import com.example.autoservice.clients.repository.ClientsRepository; +import com.example.demo.core.error.NotFoundException; + +@Service +public class ClientsService { + private final ClientsRepository repository; + + public DepartmentService(ClientsRepository repository) { + this.repository = repository; + } + + @Transactional(readOnly = true) + public List getAll() { + return StreamSupport.stream(repository.findAll().spliterator(), false).toList(); + } + + @Transactional(readOnly = true) + public ClientsEntity get(Long id) { + return repository.findById(id) + .orElseThrow(() -> new NotFoundException(ClientsEntity.class, id)); + } + + @Transactional + public ClientsEntity create(ClientsEntity entity) { + if (entity == null) { + throw new IllegalArgumentException("Entity is null"); + } + return repository.save(entity); + } + + @Transactional + public ClientsEntity update(Long id, ClientsEntity entity) { + final ClientsEntity existsEntity = get(id); + existsEntity.setFirst_Name(entity.getFirst_Name()); + existsEntity.setLast_Name(entity.setLast_Name()); + existsEntity.setMiddle_Name(entity.setMiddle_Name()); + existsEntity.setDate_Birthday(entity.setDate_Birthday()); + existsEntity.setPhone_Number(entity.setPhone_Number()); + return repository.save(existsEntity); + } + + @Transactional + public ClientsEntity delete(Long id) { + final ClientsEntity existsEntity = get(id); + repository.delete(existsEntity); + return existsEntity; + } +}