Skip to content

Commit

Permalink
Change tag images, refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
Fagorym committed May 19, 2024
1 parent 3c4293f commit 2543699
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@
@Getter
@RequiredArgsConstructor
public enum Tag implements ParseableEnum {
wifi("WI-FI", "/wifi.svg", "Около Wi-Fi"),
power("Розетки", "/zap.svg", "Около розетки"),
television("Телевизоры", "/tv.svg", "Около телевизора"),
quite("Тихое место", "/headphones.svg", "Тихое место"),
kitchen("Кухня", "/eye.svg", "Около кухни"),
dance("Танцпол", "/music.svg", "Около танцпола"),
wifi("Около окна", "sun.svg", "Около окна"),
power("Розетки", "zap.svg", "Около розетки"),
television("Телевизоры", "tv.svg", "Около телевизора"),
quite("Тихое место", "headphones.svg", "Тихое место"),
;

private final String translate;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,64 @@
package ru.nsu.fit.directors.establishmentservice.mapper;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import javax.annotation.Nonnull;
import javax.annotation.ParametersAreNonnullByDefault;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import ru.nsu.fit.directors.establishmentservice.dto.request.RequestTagDto;
import ru.nsu.fit.directors.establishmentservice.dto.response.ResponseTagDto;
import ru.nsu.fit.directors.establishmentservice.enums.Tag;
import ru.nsu.fit.directors.establishmentservice.service.ImageWorker;
import ru.nsu.fit.directors.establishmentservice.service.AmazonImageServiceImpl;
import ru.nsu.fit.directors.establishmentservice.utils.EnumUtils;

@Component
@RequiredArgsConstructor
@ParametersAreNonnullByDefault
public class TagConverter {
private final ImageWorker imageWorker;
private final AmazonImageServiceImpl imageService;

@Nonnull
public List<ResponseTagDto> toResponse(Collection<Tag> tags) {
return tags.stream()
.map(x -> new ResponseTagDto(x.getTranslate(), imageWorker.getImageFromResource(x.getAssets())))
.map(this::toResponse)
.toList();

}

@Nonnull
public List<ResponseTagDto> toResponse(Tag[] tags) {
return Arrays.stream(tags)
.map(this::toResponse)
.toList();
}

@Nonnull
public ResponseTagDto toResponse(Tag tag) {
return new ResponseTagDto(tag.getTranslate(), imageService.getByKey(tag.getAssets()));
}

@Nonnull
public ResponseTagDto toSpotResponse(Tag tag) {
return new ResponseTagDto(tag.getTranslateForSpot(), imageService.getByKey(tag.getAssets()));
}

@Nonnull
public List<ResponseTagDto> toSpotResponse(Collection<Tag> tags) {
return tags.stream()
.map(this::toSpotResponse)
.toList();
}

@Nonnull
public Set<Tag> toModel(Set<RequestTagDto> tagDtoSet) {
return tagDtoSet.stream()
.map(x -> EnumUtils.parseEnum(x.getName(), Tag.class))
.collect(Collectors.toSet());
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import ru.nsu.fit.directors.establishmentservice.exception.EstablishmentAlreadyExistsException;
import ru.nsu.fit.directors.establishmentservice.exception.EstablishmentNotFoundException;
import ru.nsu.fit.directors.establishmentservice.mapper.EstablishmentMapper;
import ru.nsu.fit.directors.establishmentservice.mapper.TagMapper;
import ru.nsu.fit.directors.establishmentservice.mapper.TagConverter;
import ru.nsu.fit.directors.establishmentservice.model.Category;
import ru.nsu.fit.directors.establishmentservice.model.Establishment;
import ru.nsu.fit.directors.establishmentservice.model.Photo;
Expand Down Expand Up @@ -70,7 +70,7 @@ public class EstablishmentServiceImpl implements EstablishmentService {
private final ImageService imageServiceImpl;
private final EstablishmentMapper establishmentMapper;
private final WorkingHoursService workingHoursService;
private final TagMapper tagMapper;
private final TagConverter tagConverter;

@Nonnull
@Override
Expand Down Expand Up @@ -140,7 +140,7 @@ public Long createEstablishmentV2(Long ownerId, RequestEstablishmentDto dto) {

@Nonnull
private Long saveEstablishmentDataV2(Establishment establishment, RequestEstablishmentDto dto) {
Set<Tag> tags = tagMapper.toModelSet(dto.getTags());
Set<Tag> tags = tagConverter.toModel(dto.getTags());
log.info("Tags were converted");
establishment.setTags(tags);
Establishment savedEstablishment = establishmentRepository.save(establishment);
Expand All @@ -166,7 +166,7 @@ public List<String> getCategories() {
@Nonnull
@Override
public List<ResponseTagDto> getTags() {
return tagMapper.toDtoList(Tag.values());
return tagConverter.toResponse(Tag.values());
}

@Nonnull
Expand Down Expand Up @@ -209,7 +209,7 @@ public List<ResponseBasicEstablishmentInfo> getEstablishmentsByIds(List<Long> id
@Override
public List<ResponseTagDto> getSpotTags(Long establishmentId) {
Establishment establishment = getEstablishmentById(establishmentId);
return tagMapper.toDtoList(establishment.getTags());
return tagConverter.toResponse(establishment.getTags());

}

Expand Down Expand Up @@ -295,7 +295,7 @@ public void deleteEstablishment(Long establishmentId) {
@Nonnull
@Override
public String getTagByName(String tagName) {
return tagMapper.modelToTagDto(EnumUtils.parseEnum(tagName, Tag.class)).getImage();
return tagConverter.toResponse(EnumUtils.parseEnum(tagName, Tag.class)).getImage();
}

private void deleteEstablishmentPhotos(Establishment establishment) {
Expand Down Expand Up @@ -330,7 +330,7 @@ private void checkEstablishmentExistence(RequestEstablishmentDto dto) {

@Nonnull
private Long saveEstablishmentData(Establishment establishment, RequestEstablishmentDto dto) {
Set<Tag> tags = tagMapper.toModelSet(dto.getTags());
Set<Tag> tags = tagConverter.toModel(dto.getTags());
log.info("Tags were converted");
establishment.setTags(tags);
Establishment savedEstablishment = establishmentRepository.save(establishment);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,18 @@ void getTags() throws Exception {

@Test
@DisplayName("Получение валидного времени бронирования заведения")
void getEstablishment() throws Exception {
void getEstablishmentValidTime() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/establishment/time?establishmentId=100"))
.andExpect(responseFromPath("http/response/establishment/valid_time.json"));
}


/*@Test
@DisplayName("Получение валидного времени бронирования заведения")
void getEstablishmentValidTimeV2() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/establishment/internal/time?establishmentId=100"))
.andExpect(responseFromPath("http/response/establishment/valid_time_internal.json"));
}
*/
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"result": [
{
"hour": "12",
"minute": "30",
"seconds": "00"
},
{
"hour": "12",
"minute": "30",
"seconds": "00"
},
{
"hour": "12",
"minute": "30",
"seconds": "00"
},
{
"hour": "12",
"minute": "30",
"seconds": "00"
},
{
"hour": "12",
"minute": "30",
"seconds": "00"
},
{
"hour": "12",
"minute": "30",
"seconds": "00"
}
],
"exception": null
}
18 changes: 8 additions & 10 deletions src/test/resources/http/response/tags/all_tags.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
{
"result": [
{
"name": "WI-FI"
"name": "Около окна",
"image": "https://storage.yandexcloud.net/budle-image-bucket/sun.svg"
},
{
"name": "Тихое место"
"name": "Тихое место",
"image": "https://storage.yandexcloud.net/budle-image-bucket/headphones.svg"
},
{
"name": "Розетки"
"name": "Розетки",
"image": "https://storage.yandexcloud.net/budle-image-bucket/zap.svg"
},
{
"name": "Телевизоры"
},
{
"name": "Кухня"
},
{
"name": "Танцпол"
"name": "Телевизоры",
"image": "https://storage.yandexcloud.net/budle-image-bucket/tv.svg"
}
],
"exception": null
Expand Down

0 comments on commit 2543699

Please sign in to comment.