Skip to content

Commit 4193b1f

Browse files
committed
fix1
1 parent 6f3ded1 commit 4193b1f

14 files changed

Lines changed: 74 additions & 23 deletions

File tree

ewm-service/src/main/java/ru/practicum/ewm/category/dto/CategoryDto.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package ru.practicum.ewm.category.dto;
22

3+
import jakarta.validation.constraints.Size;
34
import lombok.AllArgsConstructor;
45
import lombok.Data;
56
import lombok.NoArgsConstructor;
@@ -9,5 +10,7 @@
910
@AllArgsConstructor
1011
public class CategoryDto {
1112
private Long id;
13+
14+
@Size(max = 50)
1215
private String name;
1316
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package ru.practicum.ewm.category.dto;
22

33
import jakarta.validation.constraints.NotBlank;
4+
import jakarta.validation.constraints.Size;
45
import lombok.Data;
56

67
@Data
78
public class NewCategoryDto {
89
@NotBlank(message = "Название категории не может быть пустым")
10+
@Size(max = 50)
911
private String name;
1012
}

ewm-service/src/main/java/ru/practicum/ewm/compilation/mapper/CompilationMapper.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,4 @@ public static CompilationDto toCompilationDto(Compilation compilation, Set<Event
3131
compilation.getTitle()
3232
);
3333
}
34-
35-
public static CompilationDto toCompilationDto(Compilation compilation) {
36-
return new CompilationDto(
37-
compilation.getId(),
38-
null,
39-
compilation.getPinned(),
40-
compilation.getTitle()
41-
);
42-
}
4334
}

ewm-service/src/main/java/ru/practicum/ewm/compilation/service/CompilationServiceImpl.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ public CompilationDto createCompilation(NewCompilationDto dto) {
3333
Set<Event> events = (dto.getEvents() == null) ? Set.of() : Set.copyOf(eventRepository.findAllById(dto.getEvents()));
3434
Compilation compilation = CompilationMapper.toCompilation(dto, events);
3535
compilation = compilationRepository.save(compilation);
36-
return CompilationMapper.toCompilationDto(compilation);
36+
Set<EventShortDto> eventShortDtos = events.stream()
37+
.map(event -> EventMapper.toEventShortDto(event, 0L, 0L))
38+
.collect(Collectors.toSet());
39+
return CompilationMapper.toCompilationDto(compilation, eventShortDtos);
3740
}
3841

3942
@Override
@@ -47,12 +50,16 @@ public CompilationDto updateCompilation(Long compId, UpdateCompilationRequest re
4750
if (request.getPinned() != null) {
4851
compilation.setPinned(request.getPinned());
4952
}
53+
Set<Event> events = compilation.getEvents();
5054
if (request.getEvents() != null) {
51-
Set<Event> events = Set.copyOf(eventRepository.findAllById(request.getEvents()));
55+
events = Set.copyOf(eventRepository.findAllById(request.getEvents()));
5256
compilation.setEvents(events);
5357
}
5458
compilation = compilationRepository.save(compilation);
55-
return CompilationMapper.toCompilationDto(compilation);
59+
Set<EventShortDto> eventShortDtos = events.stream()
60+
.map(event -> EventMapper.toEventShortDto(event, 0L, 0L))
61+
.collect(Collectors.toSet());
62+
return CompilationMapper.toCompilationDto(compilation, eventShortDtos);
5663
}
5764

5865
@Override

ewm-service/src/main/java/ru/practicum/ewm/event/dto/EventFullDto.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package ru.practicum.ewm.event.dto;
22

3+
import com.fasterxml.jackson.annotation.JsonFormat;
34
import lombok.AllArgsConstructor;
45
import lombok.Data;
56
import lombok.NoArgsConstructor;
@@ -13,18 +14,24 @@
1314
@NoArgsConstructor
1415
@AllArgsConstructor
1516
public class EventFullDto {
17+
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
18+
private LocalDateTime createdOn;
19+
20+
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
21+
private LocalDateTime eventDate;
22+
23+
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
24+
private LocalDateTime publishedOn;
25+
1626
private Long id;
1727
private String annotation;
1828
private CategoryDto category;
1929
private Long confirmedRequests;
20-
private LocalDateTime createdOn;
2130
private String description;
22-
private LocalDateTime eventDate;
2331
private UserShortDto initiator;
2432
private Location location;
2533
private Boolean paid;
2634
private Integer participantLimit;
27-
private LocalDateTime publishedOn;
2835
private Boolean requestModeration;
2936
private String state;
3037
private String title;

ewm-service/src/main/java/ru/practicum/ewm/event/dto/EventShortDto.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package ru.practicum.ewm.event.dto;
22

3+
import com.fasterxml.jackson.annotation.JsonFormat;
34
import lombok.AllArgsConstructor;
45
import lombok.Data;
56
import lombok.NoArgsConstructor;
@@ -12,11 +13,13 @@
1213
@NoArgsConstructor
1314
@AllArgsConstructor
1415
public class EventShortDto {
16+
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
17+
private LocalDateTime eventDate;
18+
1519
private Long id;
1620
private String annotation;
1721
private CategoryDto category;
1822
private Long confirmedRequests;
19-
private LocalDateTime eventDate;
2023
private UserShortDto initiator;
2124
private Boolean paid;
2225
private String title;

ewm-service/src/main/java/ru/practicum/ewm/event/dto/NewEventDto.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import jakarta.validation.constraints.*;
55
import lombok.Data;
66
import ru.practicum.ewm.event.model.Location;
7+
import jakarta.validation.constraints.Min;
78

89
import java.time.LocalDateTime;
910

@@ -29,6 +30,8 @@ public class NewEventDto {
2930
private Location location;
3031

3132
private Boolean paid;
33+
34+
@Min(0)
3235
private Integer participantLimit;
3336
private Boolean requestModeration;
3437

ewm-service/src/main/java/ru/practicum/ewm/event/dto/UpdateEventAdminRequest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.fasterxml.jackson.annotation.JsonFormat;
44
import jakarta.validation.constraints.Future;
5+
import jakarta.validation.constraints.Min;
56
import jakarta.validation.constraints.Size;
67
import lombok.Data;
78
import ru.practicum.ewm.event.model.Location;
@@ -25,6 +26,8 @@ public class UpdateEventAdminRequest {
2526
private Location location;
2627

2728
private Boolean paid;
29+
30+
@Min(0)
2831
private Integer participantLimit;
2932
private Boolean requestModeration;
3033

ewm-service/src/main/java/ru/practicum/ewm/event/dto/UpdateEventUserRequest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import jakarta.validation.constraints.Size;
66
import lombok.Data;
77
import ru.practicum.ewm.event.model.Location;
8+
import jakarta.validation.constraints.Min;
89

910
import java.time.LocalDateTime;
1011

@@ -25,6 +26,8 @@ public class UpdateEventUserRequest {
2526
private Location location;
2627

2728
private Boolean paid;
29+
30+
@Min(0)
2831
private Integer participantLimit;
2932
private Boolean requestModeration;
3033

ewm-service/src/main/java/ru/practicum/ewm/exception/ErrorHandler.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package ru.practicum.ewm.exception;
22

3+
import jakarta.validation.ConstraintViolationException;
34
import lombok.extern.slf4j.Slf4j;
45
import org.springframework.http.HttpStatus;
56
import org.springframework.web.bind.MethodArgumentNotValidException;
7+
import org.springframework.web.bind.MissingServletRequestParameterException;
68
import org.springframework.web.bind.annotation.ExceptionHandler;
79
import org.springframework.web.bind.annotation.ResponseStatus;
810
import org.springframework.web.bind.annotation.RestControllerAdvice;
@@ -50,4 +52,25 @@ private ApiError buildApiError(HttpStatus status, String reason, String message)
5052
.timestamp(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")))
5153
.build();
5254
}
55+
56+
@ExceptionHandler(MissingServletRequestParameterException.class)
57+
@ResponseStatus(HttpStatus.BAD_REQUEST)
58+
public ApiError handleMissingParams(MissingServletRequestParameterException e) {
59+
log.error("400 {}", e.getMessage());
60+
return buildApiError(HttpStatus.BAD_REQUEST, "Отсутствует обязательный параметр", e.getMessage());
61+
}
62+
63+
@ExceptionHandler(ConstraintViolationException.class)
64+
@ResponseStatus(HttpStatus.BAD_REQUEST)
65+
public ApiError handleConstraintViolation(ConstraintViolationException e) {
66+
log.error("400 {}", e.getMessage());
67+
return buildApiError(HttpStatus.BAD_REQUEST, "Некорректное значение параметра", e.getMessage());
68+
}
69+
70+
@ExceptionHandler(ArithmeticException.class)
71+
@ResponseStatus(HttpStatus.BAD_REQUEST)
72+
public ApiError handleArithmetic(ArithmeticException e) {
73+
log.error("400 {}", e.getMessage());
74+
return buildApiError(HttpStatus.BAD_REQUEST, "Некорректные параметры пагинации", e.getMessage());
75+
}
5376
}

0 commit comments

Comments
 (0)