Skip to content

Commit

Permalink
fix: исправить недочеты в соответствии с ревью
Browse files Browse the repository at this point in the history
  • Loading branch information
OmatthewY committed Sep 18, 2024
1 parent cccc241 commit 32aee53
Show file tree
Hide file tree
Showing 34 changed files with 197 additions and 149 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import ru.practicum.category.dto.CategoryDto;
import ru.practicum.category.dto.NewCategoryDto;
import ru.practicum.category.service.CategoryService;

@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/admin")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
package ru.practicum.category.controller;

import jakarta.validation.constraints.Positive;
import jakarta.validation.constraints.PositiveOrZero;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import ru.practicum.category.dto.CategoryDto;
import ru.practicum.category.service.CategoryService;

import java.util.List;

@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/categories")
public class PublicCategoriesController {
private final CategoryService categoryService;

@GetMapping
@ResponseStatus(HttpStatus.OK)
public List<CategoryDto> getAll(@RequestParam(value = "from", defaultValue = "0") int from,
@RequestParam(value = "size", defaultValue = "10") int size) {
public List<CategoryDto> getAll(@RequestParam(value = "from", defaultValue = "0") @PositiveOrZero int from,
@RequestParam(value = "size", defaultValue = "10") @Positive int size) {
return categoryService.getAll(from, size);
}

@GetMapping("/{catId}")
@ResponseStatus(HttpStatus.OK)
public CategoryDto getById(@PathVariable("catId") long catId) {
return categoryService.getById(catId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,31 @@
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import ru.practicum.compilation.dto.CompilationDto;
import ru.practicum.compilation.dto.NewCompilationDto;
import ru.practicum.compilation.dto.UpdateCompilationRequest;
import ru.practicum.compilation.service.CompilationService;

@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/admin")
@RequestMapping("/admin/compilations")
public class AdminCompilationsController {
private final CompilationService compilationService;

@PostMapping("/compilations")
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public CompilationDto create(@Valid @RequestBody final NewCompilationDto newCompilationDto) {
return compilationService.create(newCompilationDto);
}

@DeleteMapping("/compilations/{compId}")
@DeleteMapping("/{compId}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void delete(@PathVariable long compId) {
compilationService.delete(compId);
}

@PatchMapping("/compilations/{compId}")
@ResponseStatus(HttpStatus.OK)
@PatchMapping("/{compId}")
public CompilationDto update(@PathVariable long compId,
@Valid @RequestBody UpdateCompilationRequest updateCompilationRequest) {
return compilationService.update(compId, updateCompilationRequest);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ru.practicum.compilation.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import ru.practicum.compilation.dto.CompilationDto;
import ru.practicum.compilation.dto.PublicCompilationParams;
Expand All @@ -16,7 +15,6 @@ public class PublicCompilationsController {
private final CompilationService compilationService;

@GetMapping
@ResponseStatus(HttpStatus.OK)
public List<CompilationDto> getAll(@RequestParam(value = "pinned", required = false) Boolean pinned,
@RequestParam(value = "from", defaultValue = "0") int from,
@RequestParam(value = "size", defaultValue = "10") int size) {
Expand All @@ -29,7 +27,6 @@ public List<CompilationDto> getAll(@RequestParam(value = "pinned", required = fa
}

@GetMapping("/{compId}")
@ResponseStatus(HttpStatus.OK)
public CompilationDto getById(@PathVariable("compId") long compId) {
return compilationService.getById(compId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.Set;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class NewCompilationDto {
private List<Long> events;
private Set<Long> events;

@Builder.Default
private boolean pinned = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.Set;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class UpdateCompilationRequest {
private List<Long> events;
private Set<Long> events;
private Boolean pinned;

@Size(min = 1, max = 50, message = "Длина заголовка должна составлять не менее 1 и не более 50 символов")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import lombok.*;
import ru.practicum.event.model.Event;

import java.util.Collection;
import java.util.Set;

@Entity
@Table(name = "compilations")
Expand Down Expand Up @@ -33,5 +33,5 @@ public class Compilation {
inverseJoinColumns = @JoinColumn(name = "event_id")
)
@ToString.Exclude
private Collection<Event> events;
private Set<Event> events;
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@
import ru.practicum.stat.ViewStatsDTO;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.*;
import java.util.stream.StreamSupport;

@Service
Expand All @@ -46,9 +44,9 @@ public class CompilationServiceImpl implements CompilationService {
@Override
@Transactional
public CompilationDto create(NewCompilationDto newCompilationDto) {
Collection<Event> events = new ArrayList<>();
Set<Event> events = new HashSet<>();
if (newCompilationDto.getEvents() != null) {
events = eventRepository.findByIdIn(newCompilationDto.getEvents());
events = new HashSet<>(eventRepository.findByIdIn(new ArrayList<>(newCompilationDto.getEvents())));
}
Compilation compilation = compilationMapper.toCompilation(newCompilationDto, events);
Compilation saved = compilationRepository.save(compilation);
Expand All @@ -57,11 +55,10 @@ public CompilationDto create(NewCompilationDto newCompilationDto) {
}

private List<EventShortDto> getEventShortDtos(Compilation saved) {
List<Event> compEvents = (List<Event>) saved.getEvents();
List<Event> compEvents = new ArrayList<>(saved.getEvents());

List<EventCountByRequest> eventsIdWithViews = requestRepository.findConfirmedRequestWithoutLimitCheck(compEvents);


List<String> uris = eventsIdWithViews.stream().map(ev -> "/events/" + ev.getEventId()).toList();

StatsParams statsParams = StatsParams.builder().uris(uris).unique(true).start(LocalDateTime.now().minusYears(100)).end(LocalDateTime.now()).build();
Expand Down Expand Up @@ -91,7 +88,8 @@ public void delete(long compId) {
public CompilationDto update(long compId, UpdateCompilationRequest updateCompilationRequest) {
Compilation compilation = compilationRepository.findById(compId).orElseThrow(() -> new NotFoundException("Подборка с ID = " + compId + " не найдена"));
if (updateCompilationRequest.getEvents() != null) {
compilation.setEvents(eventRepository.findByIdIn(updateCompilationRequest.getEvents()));
Set<Long> eventIds = new HashSet<>(updateCompilationRequest.getEvents());
compilation.setEvents(new HashSet<>(eventRepository.findByIdIn(new ArrayList<>(eventIds))));
}
if (updateCompilationRequest.getTitle() != null) {
compilation.setTitle(updateCompilationRequest.getTitle());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import jakarta.validation.Valid;
import jakarta.validation.constraints.Positive;
import jakarta.validation.constraints.PositiveOrZero;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
Expand All @@ -16,6 +17,7 @@
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Validated
@RequiredArgsConstructor
Expand All @@ -25,15 +27,15 @@ public class AdminEventsController {
private final EventService eventService;

@GetMapping("/events")
public List<EventFullDto> getEvents(@RequestParam(value = "users", required = false) List<Long> users,
@RequestParam(value = "states", required = false) List<EventState> states,
@RequestParam(value = "categories", required = false) List<Long> categories,
public List<EventFullDto> getEvents(@RequestParam(value = "users", required = false) Set<Long> users,
@RequestParam(value = "states", required = false) Set<EventState> states,
@RequestParam(value = "categories", required = false) Set<Long> categories,
@RequestParam(value = "rangeStart", required = false)
@DateTimeFormat(pattern = ("yyyy-MM-dd HH:mm:ss")) LocalDateTime rangeStart,
@RequestParam(value = "rangeEnd", required = false)
@DateTimeFormat(pattern = ("yyyy-MM-dd HH:mm:ss")) LocalDateTime rangeEnd,
@RequestParam(value = "from", defaultValue = "0") int from,
@RequestParam(value = "size", defaultValue = "10") int size) {
@RequestParam(value = "from", defaultValue = "0") @PositiveOrZero int from,
@RequestParam(value = "size", defaultValue = "10") @Positive int size) {

Map<String, LocalDateTime> ranges = validDate(rangeStart, rangeEnd);
AdminEventRequestParams params = AdminEventRequestParams.builder()
Expand All @@ -51,14 +53,14 @@ public List<EventFullDto> getEvents(@RequestParam(value = "users", required = fa
@PatchMapping("/events/{eventId}")
public EventFullDto updateEvent(@Positive @PathVariable long eventId,
@Valid @RequestBody UpdateEventAdminRequest dto) {

return eventService.update(eventId, dto);
}

private Map<String, LocalDateTime> validDate(LocalDateTime rangeStart, LocalDateTime rangeEnd) {
if (rangeEnd != null && rangeStart != null && rangeEnd.isBefore(rangeStart)) {
throw new WrongDateException("Дата окончания диапазона дложна быть после даты начала");
throw new WrongDateException("Дата окончания диапазона должна быть после даты начала");
}

LocalDateTime effectiveRangeStart = rangeStart != null ? rangeStart : LocalDateTime.now();
LocalDateTime effectiveRangeEnd = rangeEnd != null ? rangeEnd : effectiveRangeStart.plusYears(200);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ru.practicum.event.controller;

import jakarta.validation.Valid;
import jakarta.validation.constraints.Positive;
import jakarta.validation.constraints.PositiveOrZero;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
Expand All @@ -18,10 +20,9 @@ public class PrivateEventsController {
private final EventService eventService;

@GetMapping
@ResponseStatus(HttpStatus.OK)
public List<EventShortDto> getAll(@PathVariable("userId") long userId,
@RequestParam(value = "from", defaultValue = "0") int from,
@RequestParam(value = "size", defaultValue = "10") int size) {
@RequestParam(value = "from", defaultValue = "0") @PositiveOrZero int from,
@RequestParam(value = "size", defaultValue = "10") @Positive int size) {

return eventService.getAll(new PrivateEventParams(userId, from, size));
}
Expand All @@ -34,14 +35,12 @@ public EventFullDto create(@PathVariable("userId") long userId,
}

@GetMapping("/{eventId}")
@ResponseStatus(HttpStatus.OK)
public EventFullDto getById(@PathVariable("userId") long userId,
@PathVariable("eventId") long eventId) {
return eventService.getById(userId, eventId);
}

@PatchMapping("/{eventId}")
@ResponseStatus(HttpStatus.OK)
public EventFullDto update(@PathVariable("userId") long userId,
@PathVariable("eventId") long eventId,
@Valid @RequestBody UpdateEventUserRequest updateEventUserRequest) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package ru.practicum.event.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.constraints.Positive;
import jakarta.validation.constraints.PositiveOrZero;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import ru.practicum.client.StatClient;
import ru.practicum.event.dto.EventFullDto;
Expand All @@ -16,7 +18,9 @@
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/events")
Expand All @@ -26,18 +30,17 @@ public class PublicEventsController {
private final StatClient statClient;

@GetMapping()
@ResponseStatus(HttpStatus.OK)
public List<EventShortDto> getEventsPublic(@RequestParam(value = "text", required = false) String text,
@RequestParam(value = "categories", required = false) List<Long> categories,
@RequestParam(value = "categories", required = false) Set<Long> categories,
@RequestParam(value = "paid", required = false) Boolean paid,
@RequestParam(value = "rangeStart", required = false)
@DateTimeFormat(pattern = ("yyyy-MM-dd HH:mm:ss")) LocalDateTime rangeStart,
@RequestParam(value = "rangeEnd", required = false)
@DateTimeFormat(pattern = ("yyyy-MM-dd HH:mm:ss")) LocalDateTime rangeEnd,
@RequestParam(value = "onlyAvailable", defaultValue = "false") Boolean onlyAvailable,
@RequestParam(value = "sort", required = false) Sort sort,
@RequestParam(value = "from", defaultValue = "0") int from,
@RequestParam(value = "size", defaultValue = "10") int size,
@RequestParam(value = "from", defaultValue = "0") @PositiveOrZero int from,
@RequestParam(value = "size", defaultValue = "10") @Positive int size,
HttpServletRequest request) {

Map<String, LocalDateTime> ranges = validDate(rangeStart, rangeEnd);
Expand All @@ -58,7 +61,6 @@ public List<EventShortDto> getEventsPublic(@RequestParam(value = "text", require
}

@GetMapping("/{eventId}")
@ResponseStatus(HttpStatus.OK)
public EventFullDto getEvent(@PathVariable("eventId") long eventId, HttpServletRequest request) {
EventFullDto event = eventService.getById(eventId);
sendStats(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
import ru.practicum.event.model.EventState;

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

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class AdminEventRequestParams {
private List<Long> users;
private List<EventState> states;
private List<Long> categories;
private Set<Long> users;
private Set<EventState> states;
private Set<Long> categories;
private LocalDateTime rangeStart;
private LocalDateTime rangeEnd;
private int from;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package ru.practicum.event.dto;

public enum EventAction {
SEND_TO_REVIEW, // отправляется при юзером
CANCEL_REVIEW, // отменяется юзером
PUBLISH_EVENT, // публикуется админом
REJECT_EVENT; // отменяется админом
SEND_TO_REVIEW,
CANCEL_REVIEW,
PUBLISH_EVENT,
REJECT_EVENT
}
Loading

0 comments on commit 32aee53

Please sign in to comment.