diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..245f7b4 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,14 @@ +{ + "configurations": [ + { + "type": "java", + "name": "Spring Boot-NekontakteApplication", + "request": "launch", + "cwd": "${workspaceFolder}", + "mainClass": "com.example.nekontakte.NekontakteApplication", + "projectName": "nekontakte", + "args": "", + "envFile": "${workspaceFolder}/.env" + } + ] +} \ No newline at end of file diff --git a/nekontakte/src/main/java/com/example/nekontakte/subscribes/api/SubscribeController.java b/nekontakte/src/main/java/com/example/nekontakte/subscribes/api/SubscribeController.java index 00d3958..eb59e79 100644 --- a/nekontakte/src/main/java/com/example/nekontakte/subscribes/api/SubscribeController.java +++ b/nekontakte/src/main/java/com/example/nekontakte/subscribes/api/SubscribeController.java @@ -1,5 +1,59 @@ package com.example.nekontakte.subscribes.api; -public class SubscribeController { +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 com.example.nekontakte.subscribes.model.SubscribeEntity; +import com.example.nekontakte.subscribes.service.SubscribeService; + +import jakarta.validation.Valid; + +public class SubscribeController { + private final SubscribeService subscribeService; + private final ModelMapper modelMapper; + + public SubscribeController(SubscribeService subscribeService, ModelMapper modelMapper) { + this.modelMapper = modelMapper; + this.subscribeService = subscribeService; + } + + private SubscribeEntity toEntity(SubscribeDTO dto) { + return modelMapper.map(dto, SubscribeEntity.class); + } + + private SubscribeDTO toDTO(SubscribeEntity entity) { + return modelMapper.map(entity, SubscribeDTO.class); + } + + @GetMapping + public List getAll() { + return subscribeService.getAll().stream().map(this::toDTO).toList(); + } + + @GetMapping("/{id}") + public SubscribeDTO get(@PathVariable(name = "id") Integer id) { + return toDTO(subscribeService.get(id)); + } + + @PostMapping + public SubscribeDTO create(@RequestBody @Valid SubscribeDTO SubscribeDTO) { + return toDTO(subscribeService.create(toEntity(SubscribeDTO))); + } + + @PutMapping("/{id}") + public SubscribeDTO update(@PathVariable(name = "id") Integer id, @RequestBody SubscribeDTO SubscribeDTO) { + return toDTO(subscribeService.update(id, toEntity(SubscribeDTO))); + } + + @DeleteMapping("/{id}") + public SubscribeDTO delete(@PathVariable(name = "id") Integer id) { + return toDTO(subscribeService.delete(id)); + } } diff --git a/nekontakte/src/main/java/com/example/nekontakte/subscribes/model/SubscribeEntity.java b/nekontakte/src/main/java/com/example/nekontakte/subscribes/model/SubscribeEntity.java index 85c373b..731acbb 100644 --- a/nekontakte/src/main/java/com/example/nekontakte/subscribes/model/SubscribeEntity.java +++ b/nekontakte/src/main/java/com/example/nekontakte/subscribes/model/SubscribeEntity.java @@ -3,7 +3,6 @@ package com.example.nekontakte.subscribes.model; import java.util.Objects; import com.example.nekontakte.core.model.BaseEntity; -import com.example.nekontakte.posts.model.PostEntity; import com.example.nekontakte.users.model.UserEntity; public class SubscribeEntity extends BaseEntity { diff --git a/nekontakte/src/main/java/com/example/nekontakte/subscribes/repository/SubscribeRepository.java b/nekontakte/src/main/java/com/example/nekontakte/subscribes/repository/SubscribeRepository.java index 01b462f..7a59bda 100644 --- a/nekontakte/src/main/java/com/example/nekontakte/subscribes/repository/SubscribeRepository.java +++ b/nekontakte/src/main/java/com/example/nekontakte/subscribes/repository/SubscribeRepository.java @@ -1,5 +1,8 @@ package com.example.nekontakte.subscribes.repository; -public class SubscribeRepository { +import com.example.nekontakte.core.repository.MapRepository; +import com.example.nekontakte.subscribes.model.SubscribeEntity; + +public class SubscribeRepository extends MapRepository { } diff --git a/nekontakte/src/main/java/com/example/nekontakte/subscribes/service/SubscribeService.java b/nekontakte/src/main/java/com/example/nekontakte/subscribes/service/SubscribeService.java index 8cb05a0..92f6d6e 100644 --- a/nekontakte/src/main/java/com/example/nekontakte/subscribes/service/SubscribeService.java +++ b/nekontakte/src/main/java/com/example/nekontakte/subscribes/service/SubscribeService.java @@ -1,5 +1,41 @@ package com.example.nekontakte.subscribes.service; -public class SubscribeService { +import java.util.List; +import java.util.Optional; +import com.example.nekontakte.core.errors.NotFoundException; +import com.example.nekontakte.subscribes.model.SubscribeEntity; +import com.example.nekontakte.subscribes.repository.SubscribeRepository; + +public class SubscribeService { + private final SubscribeRepository repository; + + public SubscribeService(SubscribeRepository repository) { + this.repository = repository; + } + + public List getAll() { + return repository.getAll(); + } + + public SubscribeEntity get(Integer id) { + return Optional.ofNullable(repository.get(id)).orElseThrow(() -> new NotFoundException(id)); + } + + public SubscribeEntity create(SubscribeEntity entity) { + return repository.create(entity); + } + + public SubscribeEntity update(Integer id, SubscribeEntity entity) { + final SubscribeEntity existEntity = get(id); + existEntity.setId(entity.getId()); + existEntity.setSubscriber(entity.getSubscriber()); + existEntity.setUser(entity.getUser()); + return repository.update(existEntity); + } + + public SubscribeEntity delete(Integer id) { + final SubscribeEntity existsentity = get(id); + return repository.delete(existsentity); + } }