Skip to content

Commit

Permalink
Merge pull request #6372 from ita-social-projects/addition-custom-hab…
Browse files Browse the repository at this point in the history
…it-update-endpoint

Addition of new endpoint PUT[/habit/update/{habitId}]
  • Loading branch information
HelenSotnik authored Sep 26, 2023
2 parents a8b5e0e + f4530ac commit 252708d
Show file tree
Hide file tree
Showing 16 changed files with 522 additions and 119 deletions.
1 change: 1 addition & 0 deletions core/src/main/java/greencity/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ protected void configure(HttpSecurity http) throws Exception {
"/ownSecurity",
"/user/profile",
EVENTS + "/update",
"/habit/update/{habitId}",
HABIT_ASSIGN_ID + "/update-habit-duration",
"/habit/assign/{habitAssignId}/updateProgressNotificationHasDisplayed",
HABIT_ASSIGN_ID + "/allUserAndCustomList",
Expand Down
41 changes: 32 additions & 9 deletions core/src/main/java/greencity/controller/HabitController.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import greencity.annotations.ValidLanguage;
import greencity.constant.HttpStatuses;
import greencity.dto.PageableDto;
import greencity.dto.habit.AddCustomHabitDtoRequest;
import greencity.dto.habit.AddCustomHabitDtoResponse;
import greencity.dto.habit.CustomHabitDtoRequest;
import greencity.dto.habit.CustomHabitDtoResponse;
import greencity.dto.shoppinglistitem.ShoppingListItemDto;
import greencity.dto.habit.HabitDto;
import greencity.dto.habit.HabitVO;
Expand Down Expand Up @@ -41,6 +41,7 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;
Expand Down Expand Up @@ -214,23 +215,22 @@ public ResponseEntity<List<String>> findAllHabitsTags(@ApiIgnore @ValidLanguage
/**
* Method for creating Custom Habit.
*
* @param request {@link AddCustomHabitDtoRequest} - new custom habit dto.
* @return dto {@link AddCustomHabitDtoResponse}
* @param request {@link CustomHabitDtoRequest} - new custom habit dto.
* @return dto {@link CustomHabitDtoResponse}
*
* @author Lilia Mokhnatska.
*/
@ApiOperation(value = "Add new custom habit.")
@ResponseStatus(value = HttpStatus.CREATED)
@ApiResponses(value = {
@ApiResponse(code = 201, message = HttpStatuses.CREATED, response = AddCustomHabitDtoResponse.class),
@ApiResponse(code = 201, message = HttpStatuses.CREATED, response = CustomHabitDtoResponse.class),
@ApiResponse(code = 400, message = HttpStatuses.BAD_REQUEST),
@ApiResponse(code = 401, message = HttpStatuses.UNAUTHORIZED),
@ApiResponse(code = 404, message = HttpStatuses.NOT_FOUND),
@ApiResponse(code = 401, message = HttpStatuses.UNAUTHORIZED)
})
@PostMapping(value = "/custom",
consumes = {MediaType.APPLICATION_JSON_UTF8_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE})
public ResponseEntity<AddCustomHabitDtoResponse> addCustomHabit(
@RequestPart @Valid AddCustomHabitDtoRequest request,
public ResponseEntity<CustomHabitDtoResponse> addCustomHabit(
@RequestPart @Valid CustomHabitDtoRequest request,
@ApiParam(value = "Image of habit") @ImageValidation @RequestPart(required = false) MultipartFile image,
@ApiIgnore Principal principal) {
return ResponseEntity
Expand Down Expand Up @@ -262,4 +262,27 @@ public ResponseEntity<List<UserProfilePictureDto>> getFriendsAssignedToHabitProf
return ResponseEntity.status(HttpStatus.OK)
.body(habitService.getFriendsAssignedToHabitProfilePictures(habitId, userVO.getId()));
}

/**
* Method for updating Custom Habit.
*
* @param request {@link CustomHabitDtoRequest} - custom habit dto.
* @return dto {@link CustomHabitDtoResponse}
*
* @author Olena Sotnik.
*/
@ApiOperation(value = "Update new custom habit.")
@ApiResponses(value = {
@ApiResponse(code = 200, message = HttpStatuses.OK, response = CustomHabitDtoResponse.class),
@ApiResponse(code = 400, message = HttpStatuses.BAD_REQUEST),
@ApiResponse(code = 401, message = HttpStatuses.UNAUTHORIZED),
@ApiResponse(code = 404, message = HttpStatuses.NOT_FOUND)
})
@PutMapping(value = "/update/{habitId}")
public ResponseEntity<CustomHabitDtoResponse> updateCustomHabit(@PathVariable Long habitId,
@RequestPart @Valid CustomHabitDtoRequest request, @ApiIgnore Principal principal,
@ApiParam(value = "Image of habit") @ImageValidation @RequestPart(required = false) MultipartFile image) {
return ResponseEntity.status(HttpStatus.OK)
.body(habitService.updateCustomHabit(request, habitId, principal.getName(), image));
}
}
7 changes: 4 additions & 3 deletions core/src/test/java/greencity/ModelUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import greencity.dto.habit.HabitAssignPropertiesDto;
import greencity.dto.habit.HabitVO;
import greencity.dto.habit.UpdateUserShoppingListDto;
import greencity.dto.habit.AddCustomHabitDtoRequest;
import greencity.dto.habit.CustomHabitDtoRequest;
import greencity.dto.habit.UserShoppingAndCustomShoppingListsDto;
import greencity.dto.habitfact.HabitFactPostDto;
import greencity.dto.habitfact.HabitFactTranslationUpdateDto;
Expand Down Expand Up @@ -113,6 +113,7 @@
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -737,8 +738,8 @@ public static AddEventDtoRequest getAddEventDtoRequest() {
.build())).tags(List.of("first", "second", "third")).build();
}

public static AddCustomHabitDtoRequest getAddCustomHabitDtoRequest() {
return AddCustomHabitDtoRequest.builder()
public static CustomHabitDtoRequest getAddCustomHabitDtoRequest() {
return CustomHabitDtoRequest.builder()
.complexity(2)
.customShoppingListItemDto(List.of(
CustomShoppingListItemResponseDto.builder()
Expand Down
39 changes: 36 additions & 3 deletions core/src/test/java/greencity/controller/HabitControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import greencity.ModelUtils;
import greencity.dto.habit.AddCustomHabitDtoRequest;
import greencity.dto.habit.CustomHabitDtoRequest;
import greencity.dto.user.UserVO;
import greencity.exception.handler.CustomExceptionHandler;
import greencity.service.HabitService;
Expand All @@ -12,10 +12,10 @@
import java.util.Arrays;
import java.util.List;
import java.util.Locale;

import java.util.Optional;

import greencity.service.TagsService;
import lombok.SneakyThrows;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -36,6 +36,8 @@
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.springframework.test.web.servlet.request.MockMultipartHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.validation.Validator;

Expand Down Expand Up @@ -281,7 +283,7 @@ void getShoppingListItems() throws Exception {

@Test
void postCustomHabit() throws Exception {
AddCustomHabitDtoRequest dto = ModelUtils.getAddCustomHabitDtoRequest();
CustomHabitDtoRequest dto = ModelUtils.getAddCustomHabitDtoRequest();
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.findAndRegisterModules();

Expand All @@ -307,4 +309,35 @@ void getFriendsAssignedToHabitProfilePictures() throws Exception {

verify(habitService).getFriendsAssignedToHabitProfilePictures(habitId, null);
}

@Test
@SneakyThrows
void updateCustomHabit() {
Long habitId = 1L;
byte[] imageContent = "sampleImageData".getBytes();
MockMultipartFile imageFile = new MockMultipartFile("image", imageContent);
CustomHabitDtoRequest dto = ModelUtils.getAddCustomHabitDtoRequest();

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.findAndRegisterModules();

String requestedJson = objectMapper.writeValueAsString(dto);
MockMultipartFile jsonFile = new MockMultipartFile("request", "",
"application/json", requestedJson.getBytes());

MockMultipartHttpServletRequestBuilder builder =
MockMvcRequestBuilders.multipart(habitLink + "/update/{habitId}", habitId);
builder.with(request -> {
request.setMethod("PUT");
return request;
});
mockMvc.perform(builder
.file(jsonFile)
.file(imageFile)
.principal(principal)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());

verify(habitService).updateCustomHabit(dto, habitId, principal.getName(), imageFile);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public final class ErrorMessage {
public static final String HABIT_FACT_NOT_DELETED_BY_ID = "The habitfact does not deleted by id: ";
public static final String SPECIFICATION_VALUE_NOT_FOUND_BY_ID =
"The specification value does not exist by this id: ";
public static final String CUSTOM_HABIT_NOT_FOUND = "Custom habit does not exist by id: ";
public static final String SPECIFICATION_NOT_FOUND_BY_NAME = "The specification does not exist by this name: ";
public static final String LOCATION_IS_PRESENT = "Location is present.";
public static final String PHOTO_IS_PRESENT = "Photo is present.";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
@Getter
@Setter
@EqualsAndHashCode
public class AddCustomHabitDtoRequest {
public class CustomHabitDtoRequest {
@Min(value = 1, message = ServiceValidationConstants.HABIT_COMPLEXITY)
@Max(value = 3, message = ServiceValidationConstants.HABIT_COMPLEXITY)
@NotNull(message = ServiceValidationConstants.HABIT_COMPLEXITY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
@Getter
@Setter
@EqualsAndHashCode
public class AddCustomHabitDtoResponse {
public class CustomHabitDtoResponse {
private Long id;
private Long userId;
private String image;
Expand Down
24 changes: 18 additions & 6 deletions service-api/src/main/java/greencity/service/HabitService.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package greencity.service;

import greencity.dto.PageableDto;
import greencity.dto.habit.AddCustomHabitDtoRequest;
import greencity.dto.habit.AddCustomHabitDtoResponse;
import greencity.dto.habit.CustomHabitDtoRequest;
import greencity.dto.habit.CustomHabitDtoResponse;
import greencity.dto.habit.HabitVO;
import greencity.dto.shoppinglistitem.ShoppingListItemDto;
import greencity.dto.habit.HabitDto;
Expand Down Expand Up @@ -110,15 +110,15 @@ PageableDto<HabitDto> getAllByDifferentParameters(UserVO userVO, Pageable pageab
List<Long> addAllShoppingListItemsByListOfId(Long habitId, List<Long> listId);

/**
* Method to save {@link AddCustomHabitDtoResponse}.
* Method to save {@link CustomHabitDtoResponse}.
*
* @param addCustomHabitDtoRequest dto with {@link AddCustomHabitDtoRequest}
* @param addCustomHabitDtoRequest dto with {@link CustomHabitDtoRequest}
* entered info about field that need to edit.
* @param userEmail {@link String} - user email.
* @return {@link AddCustomHabitDtoResponse} instance.
* @return {@link CustomHabitDtoResponse} instance.
* @author Lilia Mokhnatska
*/
AddCustomHabitDtoResponse addCustomHabit(AddCustomHabitDtoRequest addCustomHabitDtoRequest, MultipartFile image,
CustomHabitDtoResponse addCustomHabit(CustomHabitDtoRequest addCustomHabitDtoRequest, MultipartFile image,
String userEmail);

/**
Expand All @@ -130,4 +130,16 @@ AddCustomHabitDtoResponse addCustomHabit(AddCustomHabitDtoRequest addCustomHabit
* @return List of friends' profile pictures.
*/
List<UserProfilePictureDto> getFriendsAssignedToHabitProfilePictures(Long habitId, Long userId);

/**
* Method to update {@link CustomHabitDtoResponse}.
*
* @param customHabitDtoRequest dto with {@link CustomHabitDtoRequest} entered
* info about field that need to edit.
* @param userEmail {@link String} - user email.
* @return {@link CustomHabitDtoResponse} instance.
* @author Olena Sotnik.
*/
CustomHabitDtoResponse updateCustomHabit(CustomHabitDtoRequest customHabitDtoRequest, Long habitId,
String userEmail, MultipartFile image);
}
12 changes: 6 additions & 6 deletions service-api/src/test/java/greencity/ModelUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import greencity.dto.event.EventDto;
import greencity.dto.eventcomment.EventCommentAuthorDto;
import greencity.dto.eventcomment.EventCommentForSendEmailDto;
import greencity.dto.habit.AddCustomHabitDtoRequest;
import greencity.dto.habit.AddCustomHabitDtoResponse;
import greencity.dto.habit.CustomHabitDtoRequest;
import greencity.dto.habit.CustomHabitDtoResponse;
import greencity.dto.habit.UserShoppingAndCustomShoppingListsDto;
import greencity.dto.newssubscriber.NewsSubscriberResponseDto;
import greencity.dto.place.PlaceNotificationDto;
Expand Down Expand Up @@ -195,17 +195,17 @@ public static UserShoppingAndCustomShoppingListsDto getUserShoppingAndCustomShop
.build();
}

public static AddCustomHabitDtoRequest getAddCustomHabitDtoRequest() {
return AddCustomHabitDtoRequest.builder()
public static CustomHabitDtoRequest getAddCustomHabitDtoRequest() {
return CustomHabitDtoRequest.builder()
.complexity(1)
.image("")
.defaultDuration(14)
.tagIds(Set.of(20L))
.build();
}

public static AddCustomHabitDtoResponse getAddCustomHabitDtoResponse() {
return AddCustomHabitDtoResponse.builder()
public static CustomHabitDtoResponse getAddCustomHabitDtoResponse() {
return CustomHabitDtoResponse.builder()
.id(1L)
.complexity(1)
.image("")
Expand Down
Loading

0 comments on commit 252708d

Please sign in to comment.