Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add controllers #57

Closed
wants to merge 15 commits into from
Closed
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
4 changes: 4 additions & 0 deletions checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,8 @@
</module>
<!-- <module name="NewlineAtEndOfFile"/>-->
<module name="SuppressWarningsFilter"/>
<module name="SuppressionFilter">
<property name="file" value="suppressions.xml"/>
<property name="optional" value="false"/>
</module>
</module>
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.20</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/ru/practicum/shareit/ShareItApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
@SpringBootApplication
public class ShareItApp {

public static void main(String[] args) {
SpringApplication.run(ShareItApp.class, args);
}

public static void main(String[] args) {
SpringApplication.run(ShareItApp.class, args);
}
}
114 changes: 114 additions & 0 deletions src/main/java/ru/practicum/shareit/aspects/LoggingAspect.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package ru.practicum.shareit.aspects;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import ru.practicum.shareit.constants.MyConstants;
import ru.practicum.shareit.item.dto.ItemDto;
import ru.practicum.shareit.user.dto.UserDto;

import java.util.List;

@Component
@Aspect
@Slf4j
public class LoggingAspect {

@AfterReturning(pointcut = MyConstants.ADD_USER_POINTCUT, returning = "userDto")
public void afterReturningAddUserAdvices(UserDto userDto) {
log.info("Пользователь " + userDto + "добавлен.");
}

@AfterThrowing(pointcut = MyConstants.ADD_USER_POINTCUT, throwing = "exception")
public void afterThrowingAddUserAdvices(JoinPoint joinPoint, Throwable exception) {
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
log.info("В методе " + methodSignature.getMethod() + " выброшено исключение: " + exception.getMessage());
}

@AfterReturning(pointcut = MyConstants.REMOVE_USER_POINTCUT, returning = "response")
public void afterReturningRemoveUserByIdAdvices(String response) {
log.info(response);
}

@AfterThrowing(pointcut = MyConstants.REMOVE_USER_POINTCUT, throwing = "exception")
public void afterThrowingRemoveUserByIdAdvices(JoinPoint joinPoint, Throwable exception) {
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
log.info("В методе " + methodSignature.getMethod() + " выброшено исключение: " + exception.getMessage());
}

@AfterReturning(pointcut = MyConstants.GET_ALL_USERS_POINTCUT)
public void afterReturningGetAllUserAdvice() {
log.info("Список всех пользователей получен.");
}

@AfterReturning(pointcut = MyConstants.UPDATE_USER_POINTCUT, returning = "userDto")
public void afterReturningUpdateUserAdvice(UserDto userDto) {
log.info("Пользователь с id:" + userDto.getId() + " обновлён.");
}

@AfterThrowing(pointcut = MyConstants.UPDATE_USER_POINTCUT, throwing = "exception")
public void afterThrowingUpdateUserAdvice(JoinPoint joinPoint, Throwable exception) {
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
log.info("В методе " + methodSignature.getMethod() + " выброшено исключение: " + exception.getMessage());
}

@AfterReturning(pointcut = MyConstants.ADD_ITEM_POINTCUT, returning = "itemDto")
public void afterReturningAddItemAdvice(ItemDto itemDto) {
log.info("Item: " + itemDto.getName() + " с id:" + itemDto.getId() + " добавлен.");
}

@AfterThrowing(pointcut = MyConstants.ADD_ITEM_POINTCUT, throwing = "exception")
public void afterThrowingAddItemAdvice(JoinPoint joinPoint, Throwable exception) {
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
log.info("В методе " + methodSignature.getMethod() + " выброшено исключение: " + exception.getMessage());
}

@AfterReturning(pointcut = MyConstants.UPDATE_ITEM_POINTCUT, returning = "itemDto")
public void afterReturningUpdateItemAdvice(ItemDto itemDto) {
log.info("Item с id:" + itemDto.getId() + " обновлён.");
}

@AfterThrowing(pointcut = MyConstants.UPDATE_ITEM_POINTCUT, throwing = "exception")
public void afterThrowingUpdateItemAdvice(JoinPoint joinPoint, Throwable exception) {
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
log.info("В методе " + methodSignature.getMethod() + " выброшено исключение: " + exception.getMessage());
}

@AfterReturning(pointcut = MyConstants.GET_ITEM_BY_ID_POINTCUT, returning = "itemDto")
public void afterReturningGetItemByIdAdvice(ItemDto itemDto) {
log.info("Получен Item: " + itemDto);
}

@AfterThrowing(pointcut = MyConstants.GET_ITEM_BY_ID_POINTCUT, throwing = "exception")
public void afterThrowingGetItemByIdAdvice(JoinPoint joinPoint, Throwable exception) {
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
log.info("В методе " + methodSignature.getMethod() + " выброшено исключение: " + exception.getMessage());
}

@AfterReturning(pointcut = MyConstants.GET_ALL_ITEM_POINTCUT)
public void afterReturningGetAllItemForOwnerAdvice() {
log.info("Список всех Item получен.");
}

@AfterThrowing(pointcut = MyConstants.GET_ALL_ITEM_POINTCUT, throwing = "exception")
public void afterThrowingGetAllItemForOwnerAdvice(JoinPoint joinPoint, Throwable exception) {
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
log.info("В методе " + methodSignature.getMethod() + " выброшено исключение: " + exception.getMessage());
}

@AfterReturning(pointcut = MyConstants.SEARCH_ITEM_POINTCUT, returning = "itemDtoList")
public void afterReturningSearchItemAdvice(List<ItemDto> itemDtoList) {
log.info("Список Item по запросу получен: " + itemDtoList);
}

@AfterThrowing(pointcut = MyConstants.SEARCH_ITEM_POINTCUT, throwing = "exception")
public void afterThrowingSearchItemAdvice(JoinPoint joinPoint, Throwable exception) {
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
log.info("В методе " + methodSignature.getMethod() + " выброшено исключение: " + exception.getMessage());
}
}

21 changes: 18 additions & 3 deletions src/main/java/ru/practicum/shareit/booking/Booking.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
package ru.practicum.shareit.booking;

/**
* TODO Sprint add-bookings.
*/
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import ru.practicum.shareit.item.model.Item;
import ru.practicum.shareit.user.User;

import java.time.LocalDateTime;

@Data
@EqualsAndHashCode
@ToString
public class Booking {

private int id;
private LocalDateTime start;
private LocalDateTime end;
private Item item;
private User booker;
private BookingStatus status;
}
8 changes: 8 additions & 0 deletions src/main/java/ru/practicum/shareit/booking/BookingStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package ru.practicum.shareit.booking;

public enum BookingStatus {
WAITING,
APPROVED,
REJECTED,
CANCELED;
}
22 changes: 19 additions & 3 deletions src/main/java/ru/practicum/shareit/booking/dto/BookingDto.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
package ru.practicum.shareit.booking.dto;

/**
* TODO Sprint add-bookings.
*/
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import ru.practicum.shareit.booking.BookingStatus;
import ru.practicum.shareit.item.model.Item;
import ru.practicum.shareit.user.User;

import java.time.LocalDateTime;

@Data
@EqualsAndHashCode
@ToString
public class BookingDto {

private int id;
private LocalDateTime start;
private LocalDateTime end;
private Item item;
private User booker;
private BookingStatus status;
}
29 changes: 29 additions & 0 deletions src/main/java/ru/practicum/shareit/constants/MyConstants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package ru.practicum.shareit.constants;

public class MyConstants {
public static final String ADD_USER_POINTCUT = "execution(public ru.practicum.shareit.user.dto.UserDto ru.practicum.shareit.user.dao.UserDaoImpl.addUser" +
"(ru.practicum.shareit.user.User))";
public static final String REMOVE_USER_POINTCUT = "execution(public String ru.practicum.shareit.user.dao.UserDaoImpl.removeUserById" +
"(*))";

public static final String GET_ALL_USERS_POINTCUT = "execution(public java.util.List<ru.practicum.shareit.user.dto.UserDto> " +
"ru.practicum.shareit.user.dao.UserDaoImpl.getAllUsers())";

public static final String UPDATE_USER_POINTCUT = "execution(public ru.practicum.shareit.user.dto.UserDto " +
"ru.practicum.shareit.user.dao.UserDaoImpl.UpdateUser(..))";

public static final String ADD_ITEM_POINTCUT = "execution(public ru.practicum.shareit.item.dto.ItemDto " +
"ru.practicum.shareit.item.dao.ItemDaoImpl.addItem(..))";

public static final String UPDATE_ITEM_POINTCUT = "execution(public ru.practicum.shareit.item.dto.ItemDto " +
"ru.practicum.shareit.item.dao.ItemDaoImpl.updateItem(..))";

public static final String GET_ITEM_BY_ID_POINTCUT = "execution(public ru.practicum.shareit.item.dto.ItemDto " +
"ru.practicum.shareit.item.dao.ItemDaoImpl.getItemById(..))";

public static final String GET_ALL_ITEM_POINTCUT = "execution(public java.util.List<ru.practicum.shareit.item.dto.ItemDto> " +
"ru.practicum.shareit.item.dao.ItemDaoImpl.getAllItemForOwner(..))";

public static final String SEARCH_ITEM_POINTCUT = "execution(public java.util.List<ru.practicum.shareit.item.dto.ItemDto> " +
"ru.practicum.shareit.item.dao.ItemDaoImpl.searchItem(..))";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package ru.practicum.shareit.exception;

public class EmailDuplicateException extends RuntimeException {

public EmailDuplicateException(String message) {
super(message);
}
}
23 changes: 23 additions & 0 deletions src/main/java/ru/practicum/shareit/exception/ErrorHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ru.practicum.shareit.exception;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class ErrorHandler {

@ExceptionHandler
@ResponseStatus(HttpStatus.NOT_FOUND)
public ErrorResponse noDataFound(NoDataFoundException e) {
return new ErrorResponse(e.getMessage(), "Данные не найдены.");
}

@ExceptionHandler
@ResponseStatus(HttpStatus.CONFLICT)
public ErrorResponse emailDuplicate(EmailDuplicateException e) {
return new ErrorResponse(e.getMessage(), "Данный email уже существует.");
}

}
22 changes: 22 additions & 0 deletions src/main/java/ru/practicum/shareit/exception/ErrorResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ru.practicum.shareit.exception;

import lombok.Data;

@Data
public class ErrorResponse {
private String error;
private String description;

public ErrorResponse(String error, String description) {
this.error = error;
this.description = description;
}

public String getError() {
return error;
}

public String getDescription() {
return description;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ru.practicum.shareit.exception;

public class NoDataFoundException extends RuntimeException {
public NoDataFoundException(String message) {
super(message);
}
}
44 changes: 39 additions & 5 deletions src/main/java/ru/practicum/shareit/item/ItemController.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,46 @@
package ru.practicum.shareit.item;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import ru.practicum.shareit.item.dto.ItemDto;
import ru.practicum.shareit.item.model.Item;
import ru.practicum.shareit.item.service.ItemService;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
* TODO Sprint add-controllers.
*/
@RestController
@RequestMapping("/items")
public class ItemController {
@Autowired
ItemService itemService;

@PostMapping
public ItemDto addItem(@RequestHeader("X-Sharer-User-Id") int userId,
@Valid @RequestBody Item item) {
return itemService.addItem(userId, item);
}

@GetMapping("/{id}")
public ItemDto getItemById(@PathVariable(name = "id") int itemId) {
return itemService.getItemDtoById(itemId);
}

@GetMapping
public List<ItemDto> getAllItemForOwner(@RequestHeader("X-Sharer-User-Id") int userId) {
return itemService.getAllItemForOwner(userId);
}

@PatchMapping("/{id}")
public ItemDto updateItem(@RequestHeader("X-Sharer-User-Id") int userId,
@PathVariable(name = "id") int itemId,
@RequestBody Map<Object, Object> fields) {
return itemService.updateItem(userId, itemId, fields);
}

@GetMapping("/search")
public List<ItemDto> searchItem(@RequestParam(name = "text") String request) {
return itemService.searchItem(request);
}
}
18 changes: 18 additions & 0 deletions src/main/java/ru/practicum/shareit/item/ItemMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ru.practicum.shareit.item;

import org.springframework.stereotype.Component;
import ru.practicum.shareit.item.dto.ItemDto;
import ru.practicum.shareit.item.model.Item;

@Component
public class ItemMapper {
public ItemDto itemDto(Item item) {
return new ItemDto()
.setId(item.getId())
.setName(item.getName())
.setOwnerId(item.getOwnerId())
.setRequest(item.getRequest())
.setDescription(item.getDescription())
.setAvailable(item.getAvailable());
}
}
Loading
Loading