Skip to content

Commit 83e0967

Browse files
committed
[main_svc] оптимизация сервисов
1 parent baf09c0 commit 83e0967

19 files changed

+548
-591
lines changed

main-service/src/main/java/ru/practicum/ewm/controllers/AdminEventController.java

+11-13
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import ru.practicum.ewm.dto.EventShortDto;
88
import ru.practicum.ewm.dto.EventFullDto;
99
import ru.practicum.ewm.dto.EventToUpdateDto;
10-
import ru.practicum.ewm.dto.GetEventsRequest;
1110

1211
import jakarta.validation.Valid;
1312
import jakarta.validation.constraints.Positive;
@@ -27,24 +26,23 @@ public class AdminEventController {
2726
public List<? extends EventShortDto> getEvents(@RequestParam(required = false) List<Long> users,
2827
@RequestParam(required = false) List<String> states,
2928
@RequestParam(required = false) List<Long> categories,
30-
@RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") String rangeStart,
31-
@RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") String rangeEnd,
29+
@RequestParam(required = false) String rangeStart,
30+
@RequestParam(required = false) String rangeEnd,
3231
@RequestParam(defaultValue = "0") @PositiveOrZero int from,
3332
@RequestParam(defaultValue = "10") @Positive int size) {
34-
return eventService.find(
35-
GetEventsRequest.builder()
36-
.categories(categories)
37-
.initiators(users)
38-
.states(states)
39-
.dateRange(rangeStart, rangeEnd)
40-
.page(GetEventsRequest.Page.of(from, size))
41-
.shortFormat(false)
42-
.build()
33+
return eventService.searchAdmin(
34+
users,
35+
states,
36+
categories,
37+
rangeStart,
38+
rangeEnd,
39+
from,
40+
size
4341
);
4442
}
4543

4644
@PatchMapping("/{eventId}")
4745
public EventFullDto updateEvent(@PathVariable long eventId, @RequestBody @Valid EventToUpdateDto updateInfo) {
48-
return eventService.updateEvent(eventId, updateInfo);
46+
return eventService.updateByAdmin(eventId, updateInfo);
4947
}
5048
}

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

+11
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.springframework.web.bind.annotation.ResponseStatus;
1010
import org.springframework.web.bind.annotation.RestControllerAdvice;
1111
import ru.practicum.dto.ErrorResponse;
12+
import ru.practicum.ewm.exceptions.IdIsAlreadyInUseException;
1213
import ru.practicum.ewm.exceptions.NotFoundException;
1314
import ru.practicum.ewm.exceptions.NotValidException;
1415

@@ -85,6 +86,16 @@ public ErrorResponse handlerNotValid(final IllegalStateException e) {
8586
e.getMessage());
8687
}
8788

89+
@ExceptionHandler
90+
@ResponseStatus(HttpStatus.CONFLICT)
91+
public ErrorResponse handlerNotValid(final IdIsAlreadyInUseException e) {
92+
log.debug(e.getMessage(), e);
93+
94+
return new ErrorResponse(
95+
"CONFLICT",
96+
e.getMessage());
97+
}
98+
8899
@ExceptionHandler
89100
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
90101
public ErrorResponse handlerOther(final Exception e) {

main-service/src/main/java/ru/practicum/ewm/controllers/EventController.java

+12-15
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,17 @@ public List<? extends EventShortDto> getEvents(@RequestParam(required = false) @
3939
@RequestParam(defaultValue = "10") @Positive int size,
4040
HttpServletRequest httpRequest) {
4141

42-
List<? extends EventShortDto> result = eventService.find(
43-
GetEventsRequest.builder()
44-
.state(EventState.PUBLISHED)
45-
.text(text)
46-
.categories(categories)
47-
.paid(paid)
48-
.location(GetEventsRequest.Location.of(lat, lon, radius))
49-
.dateRange(rangeStart, rangeEnd)
50-
.onlyAvailableForParticipation(onlyAvailable)
51-
.page(GetEventsRequest.Page.of(from, size))
52-
.shortFormat(true)
53-
.sort(sort)
54-
.build()
42+
List<? extends EventShortDto> result = eventService.searchPublic(
43+
text,
44+
categories,
45+
paid,
46+
rangeStart,
47+
rangeEnd,
48+
onlyAvailable,
49+
sort,
50+
from,
51+
size,
52+
httpRequest
5553
);
5654

5755
statisticClient.hit(httpRequest);
@@ -63,9 +61,8 @@ public List<? extends EventShortDto> getEvents(@RequestParam(required = false) @
6361

6462
@GetMapping("/{id}")
6563
public EventFullDto getEvent(@PathVariable long id, HttpServletRequest request) {
66-
EventFullDto result = eventService.findPublishedById(id);
64+
EventFullDto result = eventService.findByIdPublic(id, request);
6765
statisticClient.hit(request);
68-
eventService.addHits("/events/" + id);
6966
return result;
7067
}
7168
}

main-service/src/main/java/ru/practicum/ewm/controllers/UserEventController.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ public class UserEventController {
2121
private final EventService eventService;
2222

2323
@GetMapping
24-
public List<EventShortDto> getEvents(@PathVariable long userId,
24+
public List<EventFullDto> getEvents(@PathVariable long userId,
2525
@RequestParam(defaultValue = "0") @PositiveOrZero int from,
2626
@RequestParam(defaultValue = "10") @Positive int size) {
27-
return eventService.findUserEvents(userId, from, size);
27+
return eventService.findAllByInitiatorId(from, size, userId);
2828
}
2929

3030
@GetMapping("/{eventId}")
3131
public EventFullDto getEvent(@PathVariable long userId, @PathVariable long eventId) {
32-
return eventService.findUserEventById(userId, eventId);
32+
return eventService.findEventByInitiatorId(userId, eventId);
3333
}
3434

3535
@PostMapping
@@ -42,18 +42,18 @@ public EventFullDto addEvent(@PathVariable long userId, @Valid @RequestBody Even
4242
public EventFullDto updateEvent(@PathVariable long userId,
4343
@PathVariable long eventId,
4444
@Valid @RequestBody EventToUpdateDto request) {
45-
return eventService.updateEventByInitiator(userId, eventId, request);
45+
return eventService.updateByInitiator(userId, eventId, request);
4646
}
4747

4848
@GetMapping("/{eventId}/requests")
49-
public List<ParticipationRequestDto> getEventParticipants(@PathVariable long userId, @PathVariable long eventId) {
50-
return eventService.findUserEventParticipationRequests(userId, eventId);
49+
public List<RequestDto> getEventParticipants(@PathVariable long userId, @PathVariable long eventId) {
50+
return eventService.findRequestsByInitiatorId(userId, eventId);
5151
}
5252

5353
@PatchMapping("/{eventId}/requests")
5454
public EventUpdateStatusResultDto changeRequestStatus(@PathVariable long userId,
5555
@PathVariable long eventId,
5656
@RequestBody EventUpdateStatusRequestDto updateRequest) {
57-
return eventService.changeParticipationReqStatus(userId, eventId, updateRequest);
57+
return eventService.updateRequestStatus(userId, eventId, updateRequest);
5858
}
5959
}

main-service/src/main/java/ru/practicum/ewm/controllers/UserRequestController.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,30 @@
33
import lombok.AllArgsConstructor;
44
import org.springframework.http.HttpStatus;
55
import org.springframework.web.bind.annotation.*;
6-
import ru.practicum.ewm.dto.ParticipationRequestDto;
7-
import ru.practicum.ewm.serices.ParticipationRequestService;
6+
import ru.practicum.ewm.dto.RequestDto;
7+
import ru.practicum.ewm.serices.RequestService;
88

99
import java.util.List;
1010

1111
@RestController
1212
@AllArgsConstructor
1313
@RequestMapping(path = "/users/{userId}/requests")
1414
public class UserRequestController {
15-
private final ParticipationRequestService requestService;
15+
private final RequestService requestService;
1616

1717
@GetMapping
18-
public List<ParticipationRequestDto> getUserRequests(@PathVariable long userId) {
18+
public List<RequestDto> getUserRequests(@PathVariable long userId) {
1919
return requestService.getUserRequests(userId);
2020
}
2121

2222
@PostMapping
2323
@ResponseStatus(code = HttpStatus.CREATED)
24-
public ParticipationRequestDto addParticipationRequest(@PathVariable long userId, @RequestParam long eventId) {
25-
return requestService.addParticipationRequest(userId, eventId);
24+
public RequestDto addParticipationRequest(@PathVariable long userId, @RequestParam long eventId) {
25+
return requestService.addRequest(userId, eventId);
2626
}
2727

2828
@PatchMapping("/{requestId}/cancel")
29-
public ParticipationRequestDto cancelRequest(@PathVariable long userId, @PathVariable long requestId) {
29+
public RequestDto cancelRequest(@PathVariable long userId, @PathVariable long requestId) {
3030
return requestService.cancelOwnRequest(userId, requestId);
3131
}
3232
}

main-service/src/main/java/ru/practicum/ewm/dto/EventUpdateStatusResultDto.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
@JsonIgnoreProperties(ignoreUnknown = true)
1414
public class EventUpdateStatusResultDto {
1515
//todo одновременно обновляется один статус, поэтому держать 2 поля это дичь
16-
private List<ParticipationRequestDto> confirmedRequests;
17-
private List<ParticipationRequestDto> rejectedRequests;
16+
private List<RequestDto> confirmedRequests;
17+
private List<RequestDto> rejectedRequests;
1818

19-
public static EventUpdateStatusResultDto rejectedOnly(List<ParticipationRequestDto> requests) {
19+
public static EventUpdateStatusResultDto rejectedOnly(List<RequestDto> requests) {
2020
EventUpdateStatusResultDto result = new EventUpdateStatusResultDto();
2121
result.setRejectedRequests(requests);
2222
return result;

main-service/src/main/java/ru/practicum/ewm/dto/ParticipationRequestDto.java main-service/src/main/java/ru/practicum/ewm/dto/RequestDto.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77

88
@JsonIgnoreProperties(ignoreUnknown = true)
9-
public record ParticipationRequestDto(
9+
public record RequestDto(
1010
Long id,
1111
long requester,
1212
long event,
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package ru.practicum.ewm.entities;
22

3+
import jakarta.validation.constraints.*;
34
import lombok.*;
45

56
import jakarta.persistence.*;
@@ -18,60 +19,61 @@ public class EventEntity {
1819
@Column(name = "event_id", nullable = false)
1920
private Long id;
2021

22+
@NotBlank
23+
@Size(min = 3, max = 120)
2124
@Column(nullable = false)
2225
private String title;
2326

27+
@NotBlank
28+
@Size(min = 20, max = 2000)
2429
private String annotation;
2530

31+
@NotBlank
32+
@Size(min = 20, max = 7000)
2633
@Column(nullable = false)
2734
private String description;
2835

2936
@ManyToOne(fetch = FetchType.LAZY, optional = false)
3037
@JoinColumn(name = "cat_id", nullable = false)
3138
private CategoryEntity categoryEntity;
3239

40+
@PositiveOrZero
41+
@NotNull
3342
private int participantLimit;
3443

35-
@Builder.Default
3644
@Enumerated(EnumType.STRING)
3745
@Column(nullable = false)
38-
private EventState state = EventState.PENDING;
46+
private EventState state;
3947

40-
private boolean paid;
48+
@NotNull
49+
private Boolean paid;
4150

42-
@Column(nullable = false)
51+
@NotNull
52+
@Future
53+
@Column(name = "event_date", nullable = false)
4354
private LocalDateTime eventDate;
4455

4556
private float latitude;
4657

4758
private float longitude;
4859

49-
@Builder.Default
50-
@Column(nullable = false)
51-
private LocalDateTime createdOn = LocalDateTime.now();
60+
@Column(name = "created_on", nullable = false)
61+
private LocalDateTime createdOn;
5262

63+
@Column(name = "published_on")
5364
private LocalDateTime publishedOn;
5465

66+
@Column(name = "confirmed_requests")
67+
private long confirmedRequests;
68+
5569
@ManyToOne(fetch = FetchType.LAZY, optional = false)
5670
@JoinColumn(name = "initiator_id", nullable = false)
5771
private UserEntity initiator;
5872

59-
@Builder.Default
60-
@Column(name = "req_moderation")
61-
private boolean requestModeration = true;
62-
63-
@Transient
64-
public boolean isPublished() {
65-
return state.equals(EventState.PUBLISHED);
66-
}
67-
68-
@Transient
69-
public boolean isCanceled() {
70-
return state.equals(EventState.CANCELED);
71-
}
73+
@NotNull
74+
@Column(name = "request_moderation")
75+
private Boolean requestModeration;
7276

7377
@Transient
74-
public boolean isPending() {
75-
return state.equals(EventState.PENDING);
76-
}
78+
private long views;
7779
}

main-service/src/main/java/ru/practicum/ewm/entities/ParticipationRequestEntity.java main-service/src/main/java/ru/practicum/ewm/entities/RequestEntity.java

+3-11
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,15 @@
77

88
import java.sql.Timestamp;
99
import java.time.Instant;
10-
import java.util.Objects;
1110

1211
@Entity
1312
@Getter
1413
@Setter
15-
@Table(name = "participation_requests")
16-
public class ParticipationRequestEntity {
14+
@Table(name = "requests")
15+
public class RequestEntity {
1716
@Id
1817
@GeneratedValue(strategy = GenerationType.IDENTITY)
19-
@Column(name = "id", nullable = false)
18+
@Column(name = "requests_id", nullable = false)
2019
private Long id;
2120

2221
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@@ -32,11 +31,4 @@ public class ParticipationRequestEntity {
3231
private RequestStatus status = RequestStatus.PENDING;
3332

3433
private Timestamp created = Timestamp.from(Instant.now());
35-
36-
@Transient
37-
public boolean isDataMatchRequest(long eventId, long initiatorId) {
38-
return Objects.equals(eventEntity.getInitiator().getId(), initiatorId)
39-
&& Objects.equals(eventEntity.getId(), eventId);
40-
41-
}
4234
}
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,22 @@
11
package ru.practicum.ewm.repository;
22

3-
import org.springframework.data.domain.Pageable;
43
import org.springframework.data.jpa.repository.JpaRepository;
5-
import org.springframework.data.jpa.repository.Query;
6-
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
74
import ru.practicum.ewm.entities.EventEntity;
85

96
import java.util.List;
107
import java.util.Optional;
118

12-
public interface EventRepository extends JpaRepository<EventEntity, Long>, QuerydslPredicateExecutor<EventEntity> {
13-
@Query(" select e " +
14-
"from EventEntity e " +
15-
"where e.state like 'PUBLISHED' and id = ?1")
16-
Optional<EventEntity> findPublishedById(long eventId);
9+
public interface EventRepository extends JpaRepository<EventEntity, Long> {
10+
List<EventEntity> findAllByInitiatorId(long id);
1711

18-
List<EventEntity> findByInitiatorId(long userId, Pageable pageable);
12+
boolean existsByIdAndInitiatorId(long eventId, long initiatorId);
13+
boolean existsByCategoryEntityId(long categoryId);
1914

15+
List<EventEntity> findAllByInitiatorIdInAndCategoryEntityIdIn(List<Long> initiatorIds, List<Long> categoryIds);
2016

21-
Optional<EventEntity> findByIdAndInitiatorId(long eventId, long userId);
17+
List<EventEntity> findAllByInitiatorIdIn(List<Long> initiatorIds);
2218

23-
long countByCategoryEntityId(long categoryId);
19+
List<EventEntity> findAllByCategoryEntityIdIn(List<Long> categoryIds);
2420

25-
@Query("select e.participantLimit from EventEntity e where e.id = ?1")
26-
long findEventParticipantLimit(long eventId);
21+
List<EventEntity> findAllByIdIn(List<Long> ids);
2722
}

0 commit comments

Comments
 (0)