Skip to content

Commit

Permalink
Merge pull request #163 from gelecekbilimde/enhancement/category-mana…
Browse files Browse the repository at this point in the history
…gement

GBS-23 | Implemented category management functionalities
  • Loading branch information
agitrubard authored Oct 12, 2024
2 parents 13de2f4 + 29e67c4 commit 292ff44
Show file tree
Hide file tree
Showing 23 changed files with 609 additions and 322 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package org.gelecekbilimde.scienceplatform.post.util;
package org.gelecekbilimde.scienceplatform.media.util;

import lombok.experimental.UtilityClass;

import java.text.Normalizer;
import java.util.regex.Pattern;

@UtilityClass
public class PostUtil {
public class SlugUtil {

public String slugging(String text) {
String normalizedText = Normalizer.normalize(text, Normalizer.Form.NFD)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,100 @@
package org.gelecekbilimde.scienceplatform.post.controller;

import jakarta.validation.Valid;
import jakarta.validation.constraints.Positive;
import lombok.RequiredArgsConstructor;
import org.gelecekbilimde.scienceplatform.common.model.BasePage;
import org.gelecekbilimde.scienceplatform.common.model.response.PagingResponse;
import org.gelecekbilimde.scienceplatform.common.model.response.SuccessResponse;
import org.gelecekbilimde.scienceplatform.post.model.Category;
import org.gelecekbilimde.scienceplatform.post.model.mapper.CategoryToCategoriesResponseMapper;
import org.gelecekbilimde.scienceplatform.post.model.mapper.CategoryToResponseMapper;
import org.gelecekbilimde.scienceplatform.post.model.mapper.CategoryToSummaryResponseMapper;
import org.gelecekbilimde.scienceplatform.post.model.request.CategoryCreateRequest;
import org.gelecekbilimde.scienceplatform.post.model.request.CategoryListRequest;
import org.gelecekbilimde.scienceplatform.post.model.request.CategoryUpdateRequest;
import org.gelecekbilimde.scienceplatform.post.model.response.CategoriesResponse;
import org.gelecekbilimde.scienceplatform.post.model.response.CategoryResponse;
import org.gelecekbilimde.scienceplatform.post.model.response.CategorySummaryResponse;
import org.gelecekbilimde.scienceplatform.post.service.CategoryService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
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 java.util.List;

@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/category")
@RequestMapping("/api/v1")
class CategoryController {

private final CategoryService categoryService;


private final CategoryToResponseMapper categoryToResponseMapper = CategoryToResponseMapper.initialize();
private final CategoryToSummaryResponseMapper categoryToSummaryResponseMapper = CategoryToSummaryResponseMapper.initialize();
private final CategoryToCategoriesResponseMapper categoryToCategoriesResponseMapper = CategoryToCategoriesResponseMapper.initialize();


@PostMapping("/categories")
@PreAuthorize("hasAuthority('category:list')")
SuccessResponse<PagingResponse<CategoriesResponse>> findAll(@RequestBody @Valid CategoryListRequest listRequest) {

BasePage<Category> pageOfCategories = categoryService.findAll(listRequest);

@GetMapping
SuccessResponse<List<CategoryResponse>> getCategoryList() {
List<Category> categories = categoryService.getCategories();
List<CategoryResponse> categoryResponses = categoryToResponseMapper.map(categories);
return SuccessResponse.success(categoryResponses);
final PagingResponse<CategoriesResponse> pageResponseOfCategory = PagingResponse
.<CategoriesResponse>builder()
.of(pageOfCategories)
.content(
categoryToCategoriesResponseMapper.map(pageOfCategories.getContent())
)
.build();

return SuccessResponse.success(pageResponseOfCategory);
}

@GetMapping("/categories/summary")
SuccessResponse<List<CategorySummaryResponse>> findAllSummary() {
List<Category> categories = categoryService.findAll();
List<CategorySummaryResponse> summaryResponses = categoryToSummaryResponseMapper
.map(categories);
return SuccessResponse.success(summaryResponses);
}

@GetMapping("/category/{id}")
@PreAuthorize("hasAuthority('category:detail')")
SuccessResponse<CategoryResponse> findById(@PathVariable @Positive Long id) {
Category category = categoryService.findById(id);
CategoryResponse categoryResponse = categoryToResponseMapper.map(category);
return SuccessResponse.success(categoryResponse);
}

@PostMapping("/category")
@PreAuthorize("hasAuthority('category:create')")
SuccessResponse<Void> create(@RequestBody @Valid CategoryCreateRequest request) {
categoryService.create(request);
return SuccessResponse.success();
}

@PutMapping("/category/{id}")
@PreAuthorize("hasAuthority('category:update')")
SuccessResponse<Void> update(@PathVariable @Positive Long id, @RequestBody @Valid CategoryUpdateRequest request) {
categoryService.update(id, request);
return SuccessResponse.success();
}

@PostMapping("/create")
SuccessResponse<Void> createCategory(@RequestBody @Valid CategoryCreateRequest request) {
categoryService.createCategory(request);
@DeleteMapping("/category/{id}")
@PreAuthorize("hasAuthority('category:delete')")
SuccessResponse<Void> delete(@PathVariable @Positive Long id) {
categoryService.delete(id);
return SuccessResponse.success();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ public class Category extends BaseDomainModel {

private Long id;
private String name;
private Integer order;
private String description;
private Integer orderNumber;
private String slug;
private String icon;

private Long parentId;

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ public class CategoryEntity extends BaseEntity {
@Column(name = "name")
private String name;

@Column(name = "description")
private String description;

@Column(name = "order_number")
private Integer order;
private Integer orderNumber;

@Column(name = "slug")
private String slug;
Expand All @@ -49,6 +52,6 @@ public class CategoryEntity extends BaseEntity {
private CategoryEntity parent;

public void increaseOrder() {
this.order++;
this.orderNumber++;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import org.mapstruct.factory.Mappers;

@Mapper
public interface CategoryCreateRequestToCategoryEntityMapper extends BaseMapper<CategoryCreateRequest, CategoryEntity> {
public interface CategoryCreateRequestToEntityMapper extends BaseMapper<CategoryCreateRequest, CategoryEntity> {

static CategoryCreateRequestToCategoryEntityMapper initialize() {
return Mappers.getMapper(CategoryCreateRequestToCategoryEntityMapper.class);
static CategoryCreateRequestToEntityMapper initialize() {
return Mappers.getMapper(CategoryCreateRequestToEntityMapper.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import org.mapstruct.factory.Mappers;

@Mapper
public interface CategoryEntityToCategoryMapper extends BaseMapper<CategoryEntity, Category> {
public interface CategoryEntityToDomainMapper extends BaseMapper<CategoryEntity, Category> {

static CategoryEntityToCategoryMapper initialize() {
return Mappers.getMapper(CategoryEntityToCategoryMapper.class);
static CategoryEntityToDomainMapper initialize() {
return Mappers.getMapper(CategoryEntityToDomainMapper.class);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.gelecekbilimde.scienceplatform.post.model.mapper;

import org.gelecekbilimde.scienceplatform.common.model.mapper.BaseMapper;
import org.gelecekbilimde.scienceplatform.post.model.Category;
import org.gelecekbilimde.scienceplatform.post.model.response.CategoriesResponse;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

@Mapper
public interface CategoryToCategoriesResponseMapper extends BaseMapper<Category, CategoriesResponse> {

static CategoryToCategoriesResponseMapper initialize() {
return Mappers.getMapper(CategoryToCategoriesResponseMapper.class);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.gelecekbilimde.scienceplatform.post.model.mapper;

import org.gelecekbilimde.scienceplatform.common.model.mapper.BaseMapper;
import org.gelecekbilimde.scienceplatform.post.model.Category;
import org.gelecekbilimde.scienceplatform.post.model.response.CategorySummaryResponse;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

@Mapper
public interface CategoryToSummaryResponseMapper extends BaseMapper<Category, CategorySummaryResponse> {

static CategoryToSummaryResponseMapper initialize() {
return Mappers.getMapper(CategoryToSummaryResponseMapper.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ default PostProcessEntity mapForSaving(PostManagerControlRequest postManagerCont
.content(postManagerControlRequest.getContent())
.categoryEntity(CategoryEntity.builder()
.name(postManagerControlRequest.getCategory().getName())
.order(postManagerControlRequest.getCategory().getOrder())
.slug(postManagerControlRequest.getCategory().getSlug())
.orderNumber(postManagerControlRequest.getCategory().getOrderNumber())
.icon(postManagerControlRequest.getCategory().getIcon())
.parentId(postManagerControlRequest.getCategory().getParentId())
.build())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
package org.gelecekbilimde.scienceplatform.post.model.request;

import jakarta.validation.constraints.NotBlank;
import lombok.Builder;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
import jakarta.validation.constraints.Size;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Builder
public class CategoryCreateRequest {

@NotBlank
@Size(min = 2, max = 50)
private String name;
private Integer order;
private String slug;

@NotBlank
@Size(min = 2, max = 255)
private String description;

@NotNull
@Positive
private Integer orderNumber;

@Size(max = 50)
private String icon;

@Positive
private Long parentId;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.gelecekbilimde.scienceplatform.post.model.request;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.validation.constraints.AssertTrue;
import lombok.Getter;
import lombok.Setter;
import org.gelecekbilimde.scienceplatform.common.model.request.PagingRequest;

import java.util.Set;

@Getter
@Setter
public class CategoryListRequest extends PagingRequest {

@JsonIgnore
@AssertTrue
@Override
public boolean isOrderPropertyAccepted() {
final Set<String> acceptedFilterFields = Set.of("createdAt");
return this.isPropertyAccepted(acceptedFilterFields);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.gelecekbilimde.scienceplatform.post.model.request;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class CategoryUpdateRequest {

@NotBlank
@Size(min = 2, max = 50)
private String name;

@NotBlank
@Size(min = 2, max = 255)
private String description;

private Long parentId;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.gelecekbilimde.scienceplatform.post.model.response;

import lombok.Getter;
import lombok.Setter;

import java.time.LocalDateTime;

@Getter
@Setter
public class CategoriesResponse {

private Long id;
private Long parentId;
private String name;
private Long orderNumber;
private String slug;
private String icon;
private String createdBy;
private LocalDateTime createdAt;

}
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
package org.gelecekbilimde.scienceplatform.post.model.response;


import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

@Builder
import java.time.LocalDateTime;

@Getter
@Setter
public class CategoryResponse {
private Long id;
private String name;
private Long order;
private String description;
private Long orderNumber;
private String slug;
private String icon;

private Long parentId;
private String createdBy;
private LocalDateTime createdAt;
private String updatedBy;
private LocalDateTime updatedAt;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.gelecekbilimde.scienceplatform.post.model.response;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class CategorySummaryResponse {

private Long id;
private Long parentId;
private String name;
private Long orderNumber;
private String slug;
private String icon;

}
Loading

0 comments on commit 292ff44

Please sign in to comment.