Начал делать API для постов

This commit is contained in:
Никита Потапов 2024-03-18 19:25:27 +04:00
parent 7d5d88a16c
commit f5cfb3d95f
6 changed files with 240 additions and 1 deletions

View File

@ -0,0 +1,67 @@
package com.example.nekontakte.posts.api;
import java.util.List;
import com.example.nekontakte.core.configurations.Constants;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.nekontakte.posts.model.PostEntity;
import com.example.nekontakte.posts.service.PostService;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import jakarta.validation.Valid;
@RestController
@RequestMapping(Constants.API_URL + "/post")
public class PostController {
private final PostService postService;
private final ModelMapper modelMapper;
public PostController(PostService postService, ModelMapper modelMapper) {
this.modelMapper = modelMapper;
this.postService = postService;
}
private PostEntity toEntity(PostDTO dto) {
return modelMapper.map(dto, PostEntity.class);
}
private PostDTO toDTO(PostEntity entity) {
return modelMapper.map(entity, PostDTO.class);
}
@GetMapping
public List<PostDTO> getAll() {
return postService.getAll().stream().map(this::toDTO).toList();
}
@GetMapping("/{id}")
public PostDTO get(@PathVariable(name = "id") Integer id) {
return toDTO(postService.get(id));
}
@PostMapping
public PostDTO create(@RequestBody @Valid PostDTO PostDTO) {
return toDTO(postService.create(toEntity(PostDTO)));
}
@PutMapping("/{id}")
public PostDTO update(@PathVariable(name = "id") Integer id, @RequestBody PostDTO PostDTO) {
return toDTO(postService.update(id, toEntity(PostDTO)));
}
@DeleteMapping("/{id}")
public PostDTO delete(@PathVariable(name = "id") Integer id) {
return toDTO(postService.delete(id));
}
}

View File

@ -0,0 +1,59 @@
package com.example.nekontakte.posts.api;
import java.util.Date;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
public class PostDTO {
private Integer id;
@NotNull
private Integer userId;
@NotNull
private Date pubDate;
@NotBlank
private String image;
@NotBlank
private String html;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public Date getPubDate() {
return pubDate;
}
public void setPubDate(Date pubDate) {
this.pubDate = pubDate;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getHtml() {
return html;
}
public void setHtml(String html) {
this.html = html;
}
}

View File

@ -0,0 +1,60 @@
package com.example.nekontakte.posts.model;
import java.util.Date;
import com.example.nekontakte.core.model.BaseEntity;
public class PostEntity extends BaseEntity {
private Integer userId;
private Date pubDate;
private String image;
private String html;
public PostEntity() {
}
public PostEntity(
Integer id,
Integer userId,
Date pubDate,
String image,
String html) {
super(id);
setUserId(userId);
setPubDate(pubDate);
setImage(image);
setHtml(html);
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public Date getPubDate() {
return pubDate;
}
public void setPubDate(Date pubDate) {
this.pubDate = pubDate;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getHtml() {
return html;
}
public void setHtml(String html) {
this.html = html;
}
}

View File

@ -0,0 +1,5 @@
package com.example.nekontakte.posts.repository;
public class PostRepository {
}

View File

@ -0,0 +1,48 @@
package com.example.nekontakte.posts.service;
import java.util.List;
import org.apache.el.stream.Optional;
import com.example.nekontakte.posts.model.PostEntity;
import com.example.nekontakte.posts.repository.PostRepository;
public class PostService {
private final PostRepository repository;
public PostService(PostRepository repository) {
this.repository = repository;
}
public List<PostEntity> getAll() {
return repository.getAll();
}
public PostEntity get(Integer id) {
return Optional.ofNullable(repository.get(id)).orElseThrow(() -> new NotFoundException(id));
}
public PostEntity create(PostEntity entity) {
return repository.create(entity);
}
public PostEntity update(Integer id, PostEntity entity) {
final PostEntity existsentity = get(id);
existsentity.setUsername(entity.getUsername());
existsentity.setPassword(entity.getPassword());
existsentity.setIsAdmin(entity.getIsAdmin());
existsentity.setName(entity.getName());
existsentity.setSurname(entity.getSurname());
existsentity.setStatus(entity.getStatus());
existsentity.setBirthday(entity.getBirthday());
existsentity.setCity(entity.getCity());
existsentity.setStatus(entity.getStatus());
return repository.update(existsentity);
}
public PostEntity delete(Integer id) {
final PostEntity existsentity = get(id);
return repository.delete(existsentity);
}
}

View File

@ -1,6 +1,6 @@
package com.example.nekontakte.users.model;
import java.sql.Date;
import java.util.Date;
import java.util.Objects;
import com.example.nekontakte.core.model.BaseEntity;