fixed category in posts
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
package com.krylov.refound.dto;
|
package com.krylov.refound.dto;
|
||||||
|
|
||||||
import com.krylov.refound.entity.Image;
|
|
||||||
import com.krylov.refound.enums.PostStatus;
|
import com.krylov.refound.enums.PostStatus;
|
||||||
import com.krylov.refound.enums.PostType;
|
import com.krylov.refound.enums.PostType;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.krylov.refound.entity;
|
package com.krylov.refound.entity;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
|
import com.krylov.refound.enums.PostCategory;
|
||||||
import com.krylov.refound.enums.PostStatus;
|
import com.krylov.refound.enums.PostStatus;
|
||||||
import com.krylov.refound.enums.PostType;
|
import com.krylov.refound.enums.PostType;
|
||||||
import jakarta.persistence.CascadeType;
|
import jakarta.persistence.CascadeType;
|
||||||
@@ -32,7 +33,8 @@ public class Post {
|
|||||||
private String title;
|
private String title;
|
||||||
@Column(length = 2000)
|
@Column(length = 2000)
|
||||||
private String description;
|
private String description;
|
||||||
private String category;
|
@Enumerated(EnumType.STRING)
|
||||||
|
private PostCategory category;
|
||||||
private String city;
|
private String city;
|
||||||
private String district;
|
private String district;
|
||||||
private Double latitude;
|
private Double latitude;
|
||||||
@@ -53,5 +55,4 @@ public class Post {
|
|||||||
@OneToMany(mappedBy = "post", cascade = CascadeType.ALL, orphanRemoval = true)
|
@OneToMany(mappedBy = "post", cascade = CascadeType.ALL, orphanRemoval = true)
|
||||||
private List<Image> images = new ArrayList<>();
|
private List<Image> images = new ArrayList<>();
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
29
src/main/java/com/krylov/refound/enums/PostCategory.java
Normal file
29
src/main/java/com/krylov/refound/enums/PostCategory.java
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
package com.krylov.refound.enums;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
public enum PostCategory {
|
||||||
|
ANIMAL("Питомец"),
|
||||||
|
THING("Вещь"),
|
||||||
|
OTHER("Другое");
|
||||||
|
|
||||||
|
private final String displayName;
|
||||||
|
|
||||||
|
PostCategory(String displayName) {
|
||||||
|
this.displayName = displayName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static PostCategory fromDisplayName(String displayName) {
|
||||||
|
if (displayName == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
for (PostCategory category : PostCategory.values()) {
|
||||||
|
if (category.displayName.equals(displayName)) {
|
||||||
|
return category;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new IllegalArgumentException("No enum constant with displayName: " + displayName);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@ package com.krylov.refound.mapper;
|
|||||||
import com.krylov.refound.dto.PostRequest;
|
import com.krylov.refound.dto.PostRequest;
|
||||||
import com.krylov.refound.dto.PostResponse;
|
import com.krylov.refound.dto.PostResponse;
|
||||||
import com.krylov.refound.entity.Post;
|
import com.krylov.refound.entity.Post;
|
||||||
|
import com.krylov.refound.enums.PostCategory;
|
||||||
import org.mapstruct.Mapper;
|
import org.mapstruct.Mapper;
|
||||||
import org.mapstruct.Mapping;
|
import org.mapstruct.Mapping;
|
||||||
import com.krylov.refound.entity.Image;
|
import com.krylov.refound.entity.Image;
|
||||||
@@ -12,10 +13,12 @@ import java.util.List;
|
|||||||
@Mapper(componentModel = "spring")
|
@Mapper(componentModel = "spring")
|
||||||
public interface PostMapper {
|
public interface PostMapper {
|
||||||
|
|
||||||
|
@Mapping(source = "category", target = "category", qualifiedByName = "stringToPostCategory")
|
||||||
Post toEntity(PostRequest request);
|
Post toEntity(PostRequest request);
|
||||||
|
|
||||||
@Mapping(source = "user.login", target = "userEmail")
|
@Mapping(source = "user.login", target = "userEmail")
|
||||||
@Mapping(source = "images", target = "images", qualifiedByName = "imagesToUrls")
|
@Mapping(source = "images", target = "images", qualifiedByName = "imagesToUrls")
|
||||||
|
@Mapping(target = "category", expression = "java(post.getCategory().getDisplayName())")
|
||||||
PostResponse toResponse(Post post);
|
PostResponse toResponse(Post post);
|
||||||
|
|
||||||
@Named("imagesToUrls")
|
@Named("imagesToUrls")
|
||||||
@@ -25,4 +28,10 @@ public interface PostMapper {
|
|||||||
.map(Image::getUrl)
|
.map(Image::getUrl)
|
||||||
.toList();
|
.toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Named("stringToPostCategory")
|
||||||
|
default PostCategory stringToPostCategory(String displayName) {
|
||||||
|
if (displayName == null) return null;
|
||||||
|
return PostCategory.fromDisplayName(displayName);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import com.krylov.refound.dto.PostResponse;
|
|||||||
import com.krylov.refound.entity.Image;
|
import com.krylov.refound.entity.Image;
|
||||||
import com.krylov.refound.entity.Post;
|
import com.krylov.refound.entity.Post;
|
||||||
import com.krylov.refound.enums.ErrorCode;
|
import com.krylov.refound.enums.ErrorCode;
|
||||||
|
import com.krylov.refound.enums.PostCategory;
|
||||||
import com.krylov.refound.enums.PostStatus;
|
import com.krylov.refound.enums.PostStatus;
|
||||||
import com.krylov.refound.exception.ApiException;
|
import com.krylov.refound.exception.ApiException;
|
||||||
import com.krylov.refound.mapper.PostMapper;
|
import com.krylov.refound.mapper.PostMapper;
|
||||||
@@ -93,7 +94,9 @@ public class PostService {
|
|||||||
|
|
||||||
post.setTitle(request.getTitle());
|
post.setTitle(request.getTitle());
|
||||||
post.setDescription(request.getDescription());
|
post.setDescription(request.getDescription());
|
||||||
post.setCategory(request.getCategory());
|
if (request.getCategory() != null) {
|
||||||
|
post.setCategory(PostCategory.fromDisplayName(request.getCategory()));
|
||||||
|
}
|
||||||
post.setPhone(request.getPhone());
|
post.setPhone(request.getPhone());
|
||||||
post.setType(request.getType());
|
post.setType(request.getType());
|
||||||
if (request.getLatitude() != null && request.getLongitude() != null){
|
if (request.getLatitude() != null && request.getLongitude() != null){
|
||||||
|
|||||||
Reference in New Issue
Block a user