Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# java-explore-with-me
Template repository for ExploreWithMe project.
## 📌 Ссылка на Pull Request

https://github.com/russuAV/java-explore-with-me/pull/8
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ CategoryDto update(
@Valid @RequestBody NewCategoryDto newCategoryDto) {
return categoryService.update(catId, newCategoryDto);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ public interface CategoryMapper {
CategoryDto toCategoryDto(Category category);

Category toCategory(NewCategoryDto newCategoryDto);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@

import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.*;

@Getter
@Setter
@Entity
@Table(name = "categories")
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Category {

@Id
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package ru.practicum.category.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import ru.practicum.category.model.Category;

import java.util.List;
import java.util.Optional;

public interface CategoryRepository extends JpaRepository<Category, Long> {

boolean existsByName(String name);

Optional<Category> findByName(String name);

@Query(value = "SELECT * FROM categories ORDER BY id LIMIT :size OFFSET :from", nativeQuery = true)
List<Category> findWithOffset(@Param("from") int from, @Param("size") int size);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import ru.practicum.category.mapper.CategoryMapper;
import ru.practicum.category.repository.CategoryRepository;
import ru.practicum.category.model.Category;
import ru.practicum.category.model.CategoryDto;
import ru.practicum.category.model.NewCategoryDto;
import ru.practicum.category.repository.CategoryRepository;
import ru.practicum.event.repository.EventRepository;
import ru.practicum.exception.ConflictException;
import ru.practicum.exception.NotFoundException;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
@Slf4j
Expand Down Expand Up @@ -84,11 +83,10 @@ public CategoryDto getCategoryById(Long catId) {

@Override
public List<CategoryDto> getCategories(int from, int size) {
Pageable pageable = PageRequest.of(from / size, size);
List<Category> categories = categoryRepository.findAll(pageable).getContent();

return categories.stream()
return categoryRepository.findWithOffset(from, size)
.stream()
.map(categoryMapper::toCategoryDto)
.toList();
.collect(Collectors.toList());

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ public interface CompilationMapper {
@BeanMapping(nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)
@Mapping(target = "events", ignore = true)
void updateCompilationFromDto(UpdateCompilationRequest dto, @MappingTarget Compilation compilation);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ public class Compilation {
)
private Set<Event> events = new HashSet<>();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

import java.util.List;

public interface CompilationRepository extends JpaRepository<Compilation, Long> {
public interface CompilationRepository extends JpaRepository<Compilation, Long>, CompilationRepositoryCustom {
List<Compilation> findAllByPinned(Boolean pinned, Pageable pageable);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ru.practicum.compilation.repository;

import ru.practicum.compilation.model.Compilation;

import java.util.List;

public interface CompilationRepositoryCustom {
List<Compilation> findCompilations(Boolean pinned, int from, int size);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package ru.practicum.compilation.repository;

import jakarta.persistence.EntityManager;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Root;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;
import ru.practicum.compilation.model.Compilation;

import java.util.List;

@Repository
@RequiredArgsConstructor
public class CompilationRepositoryImpl implements CompilationRepositoryCustom {
private final EntityManager entityManager;

@Override
public List<Compilation> findCompilations(Boolean pinned, int from, int size) {
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Compilation> query = cb.createQuery(Compilation.class);
Root<Compilation> root = query.from(Compilation.class);

if (pinned != null) {
query.where(cb.equal(root.get("pinned"), pinned));
}

query.orderBy(cb.desc(root.get("id")));

return entityManager.createQuery(query)
.setFirstResult(from)
.setMaxResults(size)
.getResultList();
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package ru.practicum.compilation.service;

import jakarta.persistence.EntityManager;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Root;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -87,21 +84,7 @@ public Compilation getEntityById(Long compId) {
@Override
@Transactional(readOnly = true)
public List<CompilationDto> getAllCompilations(Boolean pinned, int from, int size, HttpServletRequest request) {
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Compilation> query = cb.createQuery(Compilation.class);
Root<Compilation> root = query.from(Compilation.class);

if (pinned != null) {
query.where(cb.equal(root.get("pinned"), pinned));
}

query.orderBy(cb.desc(root.get("id")));

List<Compilation> compilations = entityManager.createQuery(query)
.setFirstResult(from)
.setMaxResults(size)
.getResultList();

List<Compilation> compilations = compilationRepository.findCompilations(pinned, from, size);
Map<Long, Integer> viewsMap = getAllViewsForCompilations(compilations);

compilations.forEach(compilation -> {
Expand All @@ -118,7 +101,6 @@ public List<CompilationDto> getAllCompilations(Boolean pinned, int from, int siz
@Override
public CompilationDto getCompilationById(Long compId, HttpServletRequest request) {
Compilation compilation = getEntityById(compId);

Map<Long, Integer> viewsMap = getAllViewsForCompilations(List.of(compilation));

compilation.getEvents().forEach(event ->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package ru.practicum.event.comment.controller;

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import ru.practicum.event.comment.model.AdminCommentSearchRequest;
import ru.practicum.event.comment.model.CommentFullDto;
import ru.practicum.event.comment.service.CommentService;

import java.util.List;

@RestController
@RequestMapping("/admin/comments")
@RequiredArgsConstructor
public class AdminCommentController {
private final CommentService commentService;

@DeleteMapping("/{commentId}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void deleteComment(@PathVariable Long commentId) { // возможность администратором удалять комменты
commentService.deleteCommentByAdmin(commentId);
}

@GetMapping
public List<CommentFullDto> getCommentsForModeration(
@Valid @ModelAttribute AdminCommentSearchRequest adminCommentSearchRequest) {
return commentService.getCommentsForModeration(adminCommentSearchRequest);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package ru.practicum.event.comment.controller;

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import ru.practicum.event.comment.model.*;
import ru.practicum.event.comment.service.CommentService;

import java.util.List;

@RestController
@RequestMapping("/users/{userId}/comments")
@RequiredArgsConstructor
public class PrivateCommentController {
private final CommentService commentService;

@PostMapping("/events/{eventId}")
@ResponseStatus(HttpStatus.CREATED)
public CommentDto addComment(
@PathVariable Long userId,
@PathVariable Long eventId,
@Valid @RequestBody NewCommentRequest newCommentRequest) {
return commentService.addComment(userId, eventId, newCommentRequest);
}

@PatchMapping("/{commentId}")
public CommentDto updateComment(
@PathVariable Long userId,
@PathVariable Long commentId,
@Valid @RequestBody UpdateCommentRequest updateCommentRequest
) {
return commentService.updateComment(userId, commentId, updateCommentRequest);
}

@DeleteMapping("/{commentId}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void deleteComment(
@PathVariable Long userId,
@PathVariable Long commentId) {
commentService.deleteComment(userId, commentId);
}

@GetMapping
public List<CommentFullDto> getOwnComments(
@PathVariable Long userId,
@Valid @ModelAttribute UserCommentSearchRequest request) {
return commentService.getOwnComments(userId, request);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ru.practicum.event.comment.mapper;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
import ru.practicum.event.comment.model.*;
import ru.practicum.event.mapper.EventMapper;
import ru.practicum.user.mapper.UserMapper;

@Mapper(componentModel = "spring",
uses = {UserMapper.class, EventMapper.class})
public interface CommentMapper {

Comment toComment(NewCommentRequest newCommentRequest);

@Mapping(source = "author.name", target = "authorName")
CommentDto toDto(Comment comment);

@Mapping(target = "text", source = "text")
Comment update(UpdateCommentRequest updateCommentRequest, @MappingTarget Comment comment);

CommentFullDto toFullDto(Comment comment);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package ru.practicum.event.comment.model;

import jakarta.validation.constraints.AssertTrue;
import jakarta.validation.constraints.PositiveOrZero;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;

import java.time.LocalDateTime;
import java.util.List;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class AdminCommentSearchRequest {
private List<Long> eventIds;
private List<Long> authorIds;

@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime rangeStart;

@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime rangeEnd;

@PositiveOrZero
private int from = 0;

@PositiveOrZero
private int size = 10;

@AssertTrue(message = "Дата начала не может быть позже даты окончания")
public boolean isValidRange() {
return rangeStart == null || rangeEnd == null || !rangeStart.isAfter(rangeEnd);
}
}
Loading