Merge pull request #21 from SlimusMinus/add-statistics
added statistics for create and found posts
This commit is contained in:
@@ -0,0 +1,20 @@
|
|||||||
|
package com.krylov.refound.controller;
|
||||||
|
|
||||||
|
import com.krylov.refound.entity.Statistics;
|
||||||
|
import com.krylov.refound.service.StatisticsService;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
@RequestMapping("/api/v1/statistics")
|
||||||
|
public class StatisticsController {
|
||||||
|
private final StatisticsService statisticsService;
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public Statistics get() {
|
||||||
|
return statisticsService.getStatistics();
|
||||||
|
}
|
||||||
|
}
|
||||||
20
src/main/java/com/krylov/refound/entity/Statistics.java
Normal file
20
src/main/java/com/krylov/refound/entity/Statistics.java
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package com.krylov.refound.entity;
|
||||||
|
|
||||||
|
import jakarta.persistence.Entity;
|
||||||
|
import jakarta.persistence.Id;
|
||||||
|
import jakarta.persistence.Table;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "statistics")
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public class Statistics {
|
||||||
|
@Id
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
private Long postsCreated;
|
||||||
|
|
||||||
|
private Long postsFound;
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package com.krylov.refound.repository;
|
||||||
|
|
||||||
|
import com.krylov.refound.entity.Statistics;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
|
public interface StatisticsRepository extends JpaRepository<Statistics, Long> {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -35,6 +35,7 @@ import org.springframework.data.domain.Pageable;
|
|||||||
import org.springframework.data.jpa.domain.Specification;
|
import org.springframework.data.jpa.domain.Specification;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@@ -50,6 +51,7 @@ public class PostService {
|
|||||||
private final FavoriteRepository favoriteRepository;
|
private final FavoriteRepository favoriteRepository;
|
||||||
private final FileStorageService fileStorageService;
|
private final FileStorageService fileStorageService;
|
||||||
private final ContentModerationFacade contentModerationFacade;
|
private final ContentModerationFacade contentModerationFacade;
|
||||||
|
private final StatisticsService statisticsService;
|
||||||
|
|
||||||
public PostRequest create(PostRequest request, List<MultipartFile> images) {
|
public PostRequest create(PostRequest request, List<MultipartFile> images) {
|
||||||
|
|
||||||
@@ -75,6 +77,7 @@ public class PostService {
|
|||||||
imageRepository.save(img);
|
imageRepository.save(img);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
statisticsService.incrementCreated();
|
||||||
return request;
|
return request;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -150,7 +153,8 @@ public class PostService {
|
|||||||
.toList();
|
.toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public PostResponse update(Long id, PostRequest request, List<MultipartFile> images, List<String> existingUrls) {
|
public PostResponse update(Long id, PostRequest request, List<MultipartFile> images,
|
||||||
|
List<String> existingUrls) {
|
||||||
Post post = repository.findById(id)
|
Post post = repository.findById(id)
|
||||||
.orElseThrow(() -> new ApiException(
|
.orElseThrow(() -> new ApiException(
|
||||||
ErrorCode.NOT_FOUND,
|
ErrorCode.NOT_FOUND,
|
||||||
@@ -234,6 +238,7 @@ public class PostService {
|
|||||||
repository.delete(post);
|
repository.delete(post);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
public PostResponse updateStatus(Long id, String statusString) {
|
public PostResponse updateStatus(Long id, String statusString) {
|
||||||
// Find the post
|
// Find the post
|
||||||
Post post = repository.findById(id)
|
Post post = repository.findById(id)
|
||||||
@@ -264,10 +269,13 @@ public class PostService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (post.getStatus() != PostStatus.CLOSED && statusString.equals(PostStatus.CLOSED.toString())) {
|
||||||
|
statisticsService.incrementFound();
|
||||||
|
}
|
||||||
|
|
||||||
// Update status
|
// Update status
|
||||||
post.setStatus(newStatus);
|
post.setStatus(newStatus);
|
||||||
Post updated = repository.save(post);
|
Post updated = repository.save(post);
|
||||||
|
|
||||||
// Convert to response with like count
|
// Convert to response with like count
|
||||||
PostResponse response = mapper.toResponse(updated);
|
PostResponse response = mapper.toResponse(updated);
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package com.krylov.refound.service;
|
||||||
|
|
||||||
|
import com.krylov.refound.entity.Statistics;
|
||||||
|
import com.krylov.refound.repository.StatisticsRepository;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
@Transactional
|
||||||
|
public class StatisticsService {
|
||||||
|
private final StatisticsRepository repository;
|
||||||
|
|
||||||
|
public void incrementCreated() {
|
||||||
|
Statistics statistics = repository.findById(1L).orElseThrow();
|
||||||
|
statistics.setPostsCreated(statistics.getPostsCreated() + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void incrementFound() {
|
||||||
|
Statistics statistics = repository.findById(1L).orElseThrow();
|
||||||
|
statistics.setPostsFound(statistics.getPostsFound() + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Statistics getStatistics() {
|
||||||
|
return repository.findById(1L).orElseThrow();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
databaseChangeLog:
|
||||||
|
- changeSet:
|
||||||
|
id: 009-create-statistics
|
||||||
|
author: alexandr krylov
|
||||||
|
changes:
|
||||||
|
- createTable:
|
||||||
|
tableName: statistics
|
||||||
|
columns:
|
||||||
|
- column:
|
||||||
|
name: id
|
||||||
|
type: BIGINT
|
||||||
|
constraints:
|
||||||
|
primaryKey: true
|
||||||
|
nullable: false
|
||||||
|
- column:
|
||||||
|
name: posts_created
|
||||||
|
type: BIGINT
|
||||||
|
defaultValueNumeric: 0
|
||||||
|
constraints:
|
||||||
|
nullable: false
|
||||||
|
- column:
|
||||||
|
name: posts_found
|
||||||
|
type: BIGINT
|
||||||
|
defaultValueNumeric: 0
|
||||||
|
constraints:
|
||||||
|
nullable: false
|
||||||
|
- insert:
|
||||||
|
tableName: statistics
|
||||||
|
columns:
|
||||||
|
- column:
|
||||||
|
name: id
|
||||||
|
valueNumeric: 1
|
||||||
|
- column:
|
||||||
|
name: posts_created
|
||||||
|
valueNumeric: 0
|
||||||
|
- column:
|
||||||
|
name: posts_found
|
||||||
|
valueNumeric: 0
|
||||||
@@ -16,3 +16,4 @@ databaseChangeLog:
|
|||||||
- include: { file: db/changelog/add/005-add-user-avatarUrl.yaml }
|
- include: { file: db/changelog/add/005-add-user-avatarUrl.yaml }
|
||||||
- include: { file: db/changelog/create/008-create-reviews.yaml }
|
- include: { file: db/changelog/create/008-create-reviews.yaml }
|
||||||
- include: { file: db/changelog/add/013-add-rules_accepted-posts.yaml}
|
- include: { file: db/changelog/add/013-add-rules_accepted-posts.yaml}
|
||||||
|
- include: { file: db/changelog/create/009-create-statistic.yaml }
|
||||||
Reference in New Issue
Block a user