Реализован API Subscribes

This commit is contained in:
Никита Потапов 2024-04-01 15:27:36 +04:00
parent 4d9ec8408b
commit edbeef071d
5 changed files with 110 additions and 4 deletions

14
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,14 @@
{
"configurations": [
{
"type": "java",
"name": "Spring Boot-NekontakteApplication<nekontakte>",
"request": "launch",
"cwd": "${workspaceFolder}",
"mainClass": "com.example.nekontakte.NekontakteApplication",
"projectName": "nekontakte",
"args": "",
"envFile": "${workspaceFolder}/.env"
}
]
}

View File

@ -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<SubscribeDTO> 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));
}
}

View File

@ -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 {

View File

@ -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<SubscribeEntity> {
}

View File

@ -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<SubscribeEntity> 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);
}
}