Skip to content
Open
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# java-explore-with-me
Template repository for ExploreWithMe project.
[Ссылка на пул-реквест](https://github.com/MrGriv/java-explore-with-me/pull/3)
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ru.practicum.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
Expand All @@ -14,11 +15,13 @@
import java.time.format.DateTimeFormatter;
import java.util.Arrays;

@Slf4j
@RestControllerAdvice
public class ErrorHandler {
@ExceptionHandler
@ResponseStatus(HttpStatus.CONFLICT)
public ApiError handleConflictException(final ConflictException e) {
log.debug("Получен статус 409 CONFLICT {}", e.getMessage(), e);
return new ApiError(e.getMessage(),
HttpStatus.CONFLICT.toString(),
Arrays.toString(e.getStackTrace()),
Expand All @@ -28,6 +31,7 @@ public ApiError handleConflictException(final ConflictException e) {
@ExceptionHandler
@ResponseStatus(HttpStatus.NOT_FOUND)
public ApiError handleNotFoundException(final NotFoundException e) {
log.debug("Получен статус 404 NOT FOUND {}", e.getMessage(), e);
return new ApiError(e.getMessage(),
HttpStatus.NOT_FOUND.toString(),
Arrays.toString(e.getStackTrace()),
Expand All @@ -37,6 +41,7 @@ public ApiError handleNotFoundException(final NotFoundException e) {
@ExceptionHandler
@ResponseStatus(HttpStatus.CONFLICT)
public ApiError handleSqlException(final SQLException e) {
log.debug("Получен статус 409 CONFLICT {}", e.getMessage(), e);
return new ApiError(e.getMessage(),
HttpStatus.CONFLICT.toString(),
Arrays.toString(e.getStackTrace()),
Expand All @@ -46,6 +51,7 @@ public ApiError handleSqlException(final SQLException e) {
@ExceptionHandler
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ApiError handleBadRequestException(final BadRequestException e) {
log.debug("Получен статус 400 BAD REQUEST {}", e.getMessage(), e);
return new ApiError(e.getMessage(),
HttpStatus.BAD_REQUEST.toString(),
Arrays.toString(e.getStackTrace()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import ru.practicum.dto.category.CategoryDto;
import ru.practicum.dto.category.NewCategoryDto;
Expand All @@ -10,7 +11,7 @@

import javax.validation.Valid;

@RestController
@Controller
@RequiredArgsConstructor
@RequestMapping(ApiPathConstants.ADMIN_CATEGORY_PATH)
public class AdminCategoryController {
Expand All @@ -27,7 +28,7 @@ public ResponseEntity<Void> delete(@PathVariable Long id) {
}

@PatchMapping(ApiPathConstants.BY_ID_PATH)
public CategoryDto update(@PathVariable Long id,
public ResponseEntity<CategoryDto> update(@PathVariable Long id,
@Valid @RequestBody NewCategoryDto newCategoryDto) {
return categoryService.update(id, newCategoryDto);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import ru.practicum.dto.compilation.CompilationDto;
import ru.practicum.dto.compilation.NewCompilationDto;
Expand All @@ -11,7 +12,7 @@

import javax.validation.Valid;

@RestController
@Controller
@RequiredArgsConstructor
@RequestMapping(ApiPathConstants.ADMIN_COMPILATIONS_PATH)
public class AdminCompilationsController {
Expand All @@ -28,7 +29,7 @@ public ResponseEntity<Void> delete(@PathVariable Long id) {
}

@PatchMapping(ApiPathConstants.BY_ID_PATH)
public CompilationDto update(@Valid @RequestBody UpdateCompilationRequest updateCompilation,
public ResponseEntity<CompilationDto> update(@Valid @RequestBody UpdateCompilationRequest updateCompilation,
@PathVariable Long id) {
return compilationService.update(updateCompilation, id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import ru.practicum.dto.user.UserDto;
import ru.practicum.service.UserService;
Expand All @@ -10,7 +11,7 @@
import javax.validation.Valid;
import java.util.List;

@RestController
@Controller
@RequiredArgsConstructor
@RequestMapping(ApiPathConstants.ADMIN_USERS_PATH)
public class AdminUserController {
Expand All @@ -22,7 +23,7 @@ public ResponseEntity<UserDto> add(@Valid @RequestBody UserDto userDto) {
}

@GetMapping
public List<UserDto> get(@RequestParam(defaultValue = "") List<Long> ids,
public ResponseEntity<List<UserDto>> get(@RequestParam(defaultValue = "") List<Long> ids,
@RequestParam(defaultValue = "0") int from,
@RequestParam(defaultValue = "10") int size) {
return userService.get(ids, from, size);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package ru.practicum.controller.pvt;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import ru.practicum.dto.event.EventFullDto;
import ru.practicum.model.user.ShowEventsState;
import ru.practicum.service.FriendService;
import ru.practicum.util.ApiPathConstants;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping(ApiPathConstants.USER_ID_FRIEND_PATH)
public class PrivateFriendController {
private final FriendService friendService;

@PostMapping(ApiPathConstants.FRIEND_ID)
public void add(@PathVariable Long id, @PathVariable Long friendId) {
friendService.add(id, friendId);
}

@GetMapping(ApiPathConstants.FRIEND_ID)
public List<EventFullDto> getFriendParticipations(@PathVariable Long id,
@PathVariable Long friendId,
@RequestParam(defaultValue = "0") int from,
@RequestParam(defaultValue = "10") int size) {
return friendService.getFriendParticipation(id, friendId, from, size);
}

@PatchMapping
public void changeEventsVisibility(@PathVariable Long id,
@RequestParam ShowEventsState state,
@RequestParam(required = false) List<Long> events) {
friendService.changeEventsVisibility(id, state, events);
}
}
2 changes: 2 additions & 0 deletions ewm-service/src/main/java/ru/practicum/dto/user/UserDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import ru.practicum.model.user.ShowEventsState;

import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
Expand All @@ -24,4 +25,5 @@ public class UserDto {
@NotEmpty
@Size(min = 6, max = 254)
private String email;
private ShowEventsState showEventsState;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.mapstruct.Mapper;
import ru.practicum.dto.user.UserDto;
import ru.practicum.model.User;
import ru.practicum.model.user.User;

@Mapper(componentModel = "spring")
public interface UserMapper {
Expand Down
23 changes: 0 additions & 23 deletions ewm-service/src/main/java/ru/practicum/model/User.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import lombok.NoArgsConstructor;
import lombok.Setter;
import ru.practicum.model.LocationDb;
import ru.practicum.model.User;
import ru.practicum.model.user.User;

import javax.persistence.*;
import java.time.LocalDateTime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import ru.practicum.model.User;
import ru.practicum.model.user.User;
import ru.practicum.model.event.Event;

import javax.persistence.*;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ru.practicum.model.user;

public enum ShowEventsState {
ALL,
CHOSEN,
HIDE
}
35 changes: 35 additions & 0 deletions ewm-service/src/main/java/ru/practicum/model/user/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package ru.practicum.model.user;

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

import javax.persistence.*;
import java.util.List;

@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_id")
private Long id;
private String name;
private String email;
@ElementCollection
@CollectionTable(name = "friends", joinColumns = @JoinColumn(name = "user_id"))
@Column(name = "friend_id")
private List<Long> friends;
@Enumerated(EnumType.STRING)
@Column(name = "show_event_state")
private ShowEventsState showEventsState;
@ElementCollection
@CollectionTable(name = "users_events", joinColumns = @JoinColumn(name = "user_id"))
@Column(name = "event_id")
private List<Long> userEvents;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public interface CategoryService {

ResponseEntity<Void> delete(Long categoryId);

CategoryDto update(Long id, NewCategoryDto newCategoryDto);
ResponseEntity<CategoryDto> update(Long id, NewCategoryDto newCategoryDto);

List<CategoryDto> get(int from, int size);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public interface CompilationService {

ResponseEntity<Void> delete(Long compilationId);

CompilationDto update(UpdateCompilationRequest updateCompilation, Long compilationId);
ResponseEntity<CompilationDto> update(UpdateCompilationRequest updateCompilation, Long compilationId);

List<CompilationDto> get(Boolean pinned, int from, int size);

Expand Down
14 changes: 14 additions & 0 deletions ewm-service/src/main/java/ru/practicum/service/FriendService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ru.practicum.service;

import ru.practicum.dto.event.EventFullDto;
import ru.practicum.model.user.ShowEventsState;

import java.util.List;

public interface FriendService {
void add(Long userId, Long friendId);

List<EventFullDto> getFriendParticipation(Long userId, Long friendId, int from, int size);

void changeEventsVisibility(Long userId, ShowEventsState state, List<Long> events);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
public interface UserService {
ResponseEntity<UserDto> add(UserDto userDto);

List<UserDto> get(List<Long> ids, int from, int size);
ResponseEntity<List<UserDto>> get(List<Long> ids, int from, int size);

ResponseEntity<Void> delete(Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ru.practicum.dto.category.CategoryDto;
import ru.practicum.dto.category.NewCategoryDto;
import ru.practicum.exception.NotFoundException;
Expand All @@ -18,6 +19,7 @@
import java.util.List;

@Service
@Transactional
@RequiredArgsConstructor
public class CategoryServiceImpl implements CategoryService {
private final CategoryStorage categoryStorage;
Expand All @@ -37,17 +39,18 @@ public ResponseEntity<Void> delete(Long id) {
}

@Override
public CategoryDto update(Long id, NewCategoryDto newCategoryDto) {
public ResponseEntity<CategoryDto> update(Long id, NewCategoryDto newCategoryDto) {
Category updatedCategory = categoryStorage.findById(id)
.orElseThrow(() -> new NotFoundException("Category: Категория с id=" + id + " не найдена"));
if (!updatedCategory.getName().equals(newCategoryDto.getName())) {
updatedCategory.setName(newCategoryDto.getName());
return categoryMapper.toDto(categoryStorage.save(updatedCategory));
return new ResponseEntity<>(categoryMapper.toDto(categoryStorage.save(updatedCategory)), HttpStatus.OK);
}
return categoryMapper.toDto(updatedCategory);
return new ResponseEntity<>(categoryMapper.toDto(updatedCategory), HttpStatus.OK);
}

@Override
@Transactional(readOnly = true)
public List<CategoryDto> get(int from, int size) {
PageRequest page = PageRequest.of(from > 0 ? from / size : 0, size);
Page<Category> categories = categoryStorage.findAll(page);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ru.practicum.dto.compilation.CompilationDto;
import ru.practicum.dto.compilation.NewCompilationDto;
import ru.practicum.dto.compilation.UpdateCompilationRequest;
Expand All @@ -26,6 +27,7 @@
import java.util.stream.Collectors;

@Service
@Transactional
@RequiredArgsConstructor
public class CompilationServiceImpl implements CompilationService {
private final CompilationStorage compilationStorage;
Expand All @@ -50,7 +52,7 @@ public ResponseEntity<Void> delete(Long compilationId) {
}

@Override
public CompilationDto update(UpdateCompilationRequest updateCompilation,
public ResponseEntity<CompilationDto> update(UpdateCompilationRequest updateCompilation,
Long compilationId) {
Compilation compilation = compilationStorage.findById(compilationId)
.orElseThrow(() -> new NotFoundException("Compilation: Список событий с id=" + compilationId +
Expand All @@ -65,10 +67,11 @@ public CompilationDto update(UpdateCompilationRequest updateCompilation,
List<Event> events = eventStorage.findAllByIdIn(savedCompilation.getEvents());
CompilationDto compilationDto = compilationMapper.toDto(savedCompilation);
compilationDto.setEvents(events.stream().map(eventMapper::toShortDto).collect(Collectors.toList()));
return compilationDto;
return new ResponseEntity<>(compilationDto, HttpStatus.OK);
}

@Override
@Transactional(readOnly = true)
public List<CompilationDto> get(Boolean pinned, int from, int size) {
PageRequest page = PageRequest.of(from > 0 ? from / size : 0, size);
Page<Compilation> compilations;
Expand Down
Loading