Skip to content

Commit

Permalink
refactor: EventFullDto, EventShortDto, LocationDto, ParticipationRequ…
Browse files Browse the repository at this point in the history
…estDto, EventMapper
  • Loading branch information
SergeyMalyuga committed Jan 16, 2024
1 parent ac9afec commit b3f047d
Show file tree
Hide file tree
Showing 17 changed files with 99 additions and 141 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import ru.practicum.exploreWithMe.entity.Category;
import ru.practicum.exploreWithMe.entity.EventState;
import ru.practicum.exploreWithMe.entity.Location;

import java.time.LocalDateTime;

Expand All @@ -19,14 +17,13 @@
@AllArgsConstructor
public class EventFullDto {


private int id;
private String annotation;
private Category category;
private CategoryDto category;
private String description;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime eventDate;
private Location location;
private LocationDto location;
private boolean paid;
private int participantLimit;
private Boolean requestModeration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class EventShortDto {

private int id;
private String annotation;
private Category category;
private CategoryDto category;
private int confirmedRequests;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime eventDate;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
package ru.practicum.exploreWithMe.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;

@Getter
@Setter
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor
public class LocationDto {

private int id;
private double lat;
private double lon;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import ru.practicum.exploreWithMe.entity.Event;
import ru.practicum.exploreWithMe.entity.RequestStatus;
import ru.practicum.exploreWithMe.entity.User;

import java.time.LocalDateTime;

Expand All @@ -19,11 +17,10 @@
@AllArgsConstructor
public class ParticipationRequestDto {


private int id;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime created;
private Event event;
private User requester;
private EventFullDto event;
private UserDto requester;
private RequestStatus requestStatus;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ public class Event {
private int id;
@Column(name = "annotation", nullable = false)
private String annotation;
@ManyToOne
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "category_id", nullable = false)
private Category category;
@Column(name = "description")
private String description;
@Column(name = "event_date", nullable = false)
private LocalDateTime eventDate;
@ManyToOne
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "location_id", nullable = false)
private Location location;
@Column(name = "paid", nullable = false)
Expand All @@ -45,7 +45,7 @@ public class Event {
private int confirmedRequests;
@Column(name = "created_on")
private LocalDateTime createdOn;
@ManyToOne(cascade = CascadeType.ALL)
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "initiator_id", nullable = false)
private User initiator;
@Column(name = "published_on")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ public class ParticipationRequest {
private int id;
@Column(name = "created")
private LocalDateTime created;
@ManyToOne(cascade = CascadeType.ALL)
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "event_id")
private Event event;
@ManyToOne(cascade = CascadeType.ALL)
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "requester_id")
private User requester;
@Enumerated(EnumType.STRING)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ru.practicum.exploreWithMe.mapper;

import org.springframework.beans.factory.annotation.Autowired;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import ru.practicum.exploreWithMe.dto.EventFullDto;
import ru.practicum.exploreWithMe.dto.EventShortDto;
Expand All @@ -11,22 +11,24 @@
import java.time.LocalDateTime;

@Component
@RequiredArgsConstructor
public class EventMapper {

@Autowired
private UserMapper userMapper;
private final UserMapper userMapper;
private final CategoryMapper categoryMapper;
private final LocationMapper locationMapper;

public EventFullDto convertToEventFullDto(Event event) {
return new EventFullDto().setEventDate(event.getEventDate())
.setAnnotation(event.getAnnotation())
.setId(event.getId())
.setCategory(event.getCategory())
.setCategory(categoryMapper.convertToCategoryDto(event.getCategory()))
.setCreatedOn(event.getCreatedOn())
.setInitiator(userMapper.convertToUserShortDto(event.getInitiator()))
.setDescription(event.getDescription())
.setPaid(event.isPaid())
.setConfirmedRequests(event.getConfirmedRequests())
.setLocation(event.getLocation())
.setLocation(locationMapper.convertToLocationDto(event.getLocation()))
.setParticipantLimit(event.getParticipantLimit())
.setPublishedOn(event.getPublishedOn())
.setRequestModeration(event.getRequestModeration())
Expand Down Expand Up @@ -56,13 +58,12 @@ public Event convertToEvent(NewEventDto newEventDto) {
public EventShortDto convertToEventShortDto(Event event) {
return new EventShortDto().setId(event.getId())
.setAnnotation(event.getAnnotation())
.setCategory(event.getCategory())
.setCategory(categoryMapper.convertToCategoryDto(event.getCategory()))
.setConfirmedRequests(event.getConfirmedRequests())
.setEventDate(event.getEventDate())
.setInitiator(userMapper.convertToUserShortDto(event.getInitiator()))
.setPaid(event.isPaid())
.setTitle(event.getTitle())
.setViews(event.getViews());

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ru.practicum.exploreWithMe.mapper;

import org.springframework.stereotype.Component;
import ru.practicum.exploreWithMe.dto.LocationDto;
import ru.practicum.exploreWithMe.entity.Location;

@Component
public class LocationMapper {

public LocationDto convertToLocationDto(Location location) {
return new LocationDto().setId(location.getId()).setLat(location.getLat()).setLon(location.getLon());
}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,12 @@
package ru.practicum.exploreWithMe.mapper;

import org.springframework.stereotype.Component;
import ru.practicum.exploreWithMe.dto.ParticipationRequestDto;
import ru.practicum.exploreWithMe.dto.ResponseRequestDto;
import ru.practicum.exploreWithMe.entity.ParticipationRequest;

@Component
public class RequestMapper {

public ParticipationRequest convertToRequest(ParticipationRequestDto requestDto) {
return new ParticipationRequest().setCreated(requestDto.getCreated())
.setRequester(requestDto.getRequester())
.setRequestStatus(requestDto.getRequestStatus())
.setEvent(requestDto.getEvent());
}

public ParticipationRequestDto convertToRequestDto(ParticipationRequest request) {
return new ParticipationRequestDto().setId(request.getId())
.setCreated(request.getCreated())
.setRequester(request.getRequester())
.setRequestStatus(request.getRequestStatus())
.setEvent(request.getEvent());
}

public ResponseRequestDto convertToResponseRequest(ParticipationRequest request) {
return new ResponseRequestDto().setId(request.getId())
.setEvent(request.getEvent().getId())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import ru.practicum.exploreWithMe.entity.Category;

public interface CategoryRepository extends JpaRepository<Category, Integer> {

@Query(value = "SELECT c FROM Category AS c")
Page<Category> findAllCategoriesWithPagination(Pageable pageable);
Page<Category> findAll(Pageable pageable);

Category findByName(String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@

public interface CompilationRepository extends JpaRepository<Compilation, Integer> {

@Query(value = "SELECT c FROM Compilation AS c WHERE c.pinned = :pinned")
public List<Compilation> findByPinned(@Param("pinned") Boolean pinned, Pageable pageable);

@Query(value = "SELECT c FROM Compilation AS c")
public List<Compilation> findByAllPagination(Pageable pageable);
@Query(value = "SELECT c FROM Compilation AS c WHERE (:pinned IS NULL OR c.pinned = :pinned)")
List<Compilation> findByAllPagination(@Param("pinned") Boolean pinned, Pageable pageable);

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ru.practicum.exploreWithMe.repository;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
Expand All @@ -11,11 +10,8 @@

public interface UserRepository extends JpaRepository<User, Integer> {

@Query(value = "SELECT u FROM User AS u WHERE u.id IN (:ids)")
List<User> findAllUsersWithUid(@Param("ids") List<Integer> uid, Pageable pageable);

@Query(value = "SELECT u FROM User AS u")
Page<User> findAllUsersWithoutUid(Pageable pageable);
@Query(value = "SELECT u FROM User AS u WHERE (:ids IS NULL OR u.id IN (:ids))")
List<User> findAllUsersWithPagination(@Param("ids") List<Integer> uid, Pageable pageable);

User findByEmail(String email);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ru.practicum.exploreWithMe.service;

import org.springframework.beans.factory.annotation.Autowired;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import ru.practicum.exploreWithMe.dto.CategoryDto;
Expand All @@ -15,16 +15,12 @@
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class CategoryServiceImp implements CategoryService {

@Autowired
private CategoryRepository categoryRepository;

@Autowired
private EventRepository eventRepository;

@Autowired
private CategoryMapper categoryMapper;
private final CategoryRepository categoryRepository;
private final EventRepository eventRepository;
private final CategoryMapper categoryMapper;

@Override
public CategoryDto addCategory(CategoryDto categoryDto) {
Expand Down Expand Up @@ -67,7 +63,7 @@ private Category checkTheExistenceCategory(int categoryId) {
}

private List<CategoryDto> getCategoriesWithFromSizeParam(Integer from, Integer size) {
return categoryRepository.findAllCategoriesWithPagination(PageRequest.of(
return categoryRepository.findAll(PageRequest.of(
(int) Math.ceil((double) from / size), size)).getContent().stream()
.map(e -> categoryMapper.convertToCategoryDto(e)).collect(Collectors.toList());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ru.practicum.exploreWithMe.service;

import org.springframework.beans.factory.annotation.Autowired;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import ru.practicum.exploreWithMe.dto.CompilationDto;
Expand All @@ -17,14 +17,12 @@
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class CompilationServiceImp implements CompilationService {

@Autowired
private CompilationRepository compilationRepository;
@Autowired
private EventRepository eventRepository;
@Autowired
private CompilationMapper compilationMapper;
private final CompilationRepository compilationRepository;
private final EventRepository eventRepository;
private final CompilationMapper compilationMapper;

@Override
public CompilationDto addCompilation(NewCompilationDto newCompilation) {
Expand Down Expand Up @@ -54,12 +52,7 @@ public CompilationDto updateCompilation(int compId, NewCompilationDto newCompila

@Override
public List<CompilationDto> getCompilations(Boolean pinned, Integer from, Integer size) {
if (pinned == null) {
return compilationRepository.findByAllPagination(PageRequest.of((int)
Math.ceil((double) from / size), size)).stream().map(e -> compilationMapper
.convertToCompilationDto(e)).collect(Collectors.toList());
}
return compilationRepository.findByPinned(pinned, PageRequest.of((int)
return compilationRepository.findByAllPagination(pinned, PageRequest.of((int)
Math.ceil((double) from / size), size)).stream().map(e -> compilationMapper
.convertToCompilationDto(e)).collect(Collectors.toList());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ru.practicum.exploreWithMe.service;

import org.springframework.beans.factory.annotation.Autowired;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import ru.practicum.exploreWithMe.client.StatClient;
Expand All @@ -17,31 +17,17 @@
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class EventServiceImp implements EventService {

@Autowired
private EventRepository eventRepository;

@Autowired
private UserRepository userRepository;

@Autowired
private RequestRepository requestRepository;

@Autowired
private LocationRepository locationRepository;

@Autowired
private CategoryRepository categoryRepository;

@Autowired
private StatClient statClient;

@Autowired
EventMapper eventMapper;

@Autowired
RequestMapper requestMapper;
private final EventRepository eventRepository;
private final UserRepository userRepository;
private final RequestRepository requestRepository;
private final LocationRepository locationRepository;
private final CategoryRepository categoryRepository;
private final StatClient statClient;
private final EventMapper eventMapper;
private final RequestMapper requestMapper;

@Override
public EventFullDto addEvent(NewEventDto newEventDto, int userId) {
Expand Down
Loading

0 comments on commit b3f047d

Please sign in to comment.