Skip to content

Commit

Permalink
Added TypesController for get all types and statuses
Browse files Browse the repository at this point in the history
  • Loading branch information
IhorTrokhymchuk committed Apr 19, 2024
1 parent 0bb0cc4 commit 966e93c
Show file tree
Hide file tree
Showing 16 changed files with 206 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
.csrf(AbstractHttpConfigurer::disable)
.authorizeHttpRequests(
auth -> auth.requestMatchers("auth/**", "/error", "/swagger-ui**",
"payments/success/**", "payments/cancel/**")
"payments/success/**", "payments/cancel/**", "types/**")
.permitAll()
.anyRequest()
.authenticated()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.example.bookingappliation.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.example.bookingappliation.dto.accommodations.response.AccommodationTypeDto;
import org.example.bookingappliation.dto.accommodations.response.AmenityTypeDto;
import org.example.bookingappliation.dto.accommodations.response.SizeTypeDto;
import org.example.bookingappliation.dto.bookingstatuses.response.BookingStatusDto;
import org.example.bookingappliation.dto.paymentstatuses.response.PaymentStatusDto;
import org.example.bookingappliation.service.AccommodationTypeService;
import org.example.bookingappliation.service.AmenityTypeService;
import org.example.bookingappliation.service.BookingStatusService;
import org.example.bookingappliation.service.PaymentStatusService;
import org.example.bookingappliation.service.SizeTypeService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "Types manage", description = "Endpoints for get type")
@RestController
@RequestMapping(value = "/types")
@RequiredArgsConstructor
public class TypesController {
private final AccommodationTypeService accommodationTypeService;
private final AmenityTypeService amenityTypeService;
private final SizeTypeService sizeTypeService;
private final PaymentStatusService paymentStatusService;
private final BookingStatusService bookingStatusService;

@GetMapping("/accommodation")
@Operation(summary = "Get all accommodation types",
description = "Get all accommodation types dto for front end")
public List<AccommodationTypeDto> getAllAccommodationTypes() {
return accommodationTypeService.getAll();
}

@GetMapping("/amenity")
@Operation(summary = "Get all amenity types",
description = "Get all amenity types dto for front end")
public List<AmenityTypeDto> getAllAmenityTypes() {
return amenityTypeService.getAll();
}

@GetMapping("/size")
@Operation(summary = "Get all size types",
description = "Get all size types dto for front end")
public List<SizeTypeDto> getAllSizeTypes() {
return sizeTypeService.getAlL();
}

@GetMapping("/payment-status")
@Operation(summary = "Get all payment status",
description = "Get all payment status dto for front end")
public List<PaymentStatusDto> getAllPaymentStatuses() {
return paymentStatusService.getAll();
}

@GetMapping("/booking-status")
@Operation(summary = "Get all booking status",
description = "Get all booking status dto for front end")
public List<BookingStatusDto> getAllBookingStatuses() {
return bookingStatusService.getAll();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.example.bookingappliation.dto.roletypes.response;

import lombok.Data;

@Data
public class RoleTypeDto {
private Long id;
private String name;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

import java.util.Set;
import lombok.Data;
import org.example.bookingappliation.dto.roletypes.response.RoleTypeDto;

@Data
public class UserResponseDto {
private String email;
private String firstName;
private String lastName;
private Set<String> roleNames;
private Set<RoleTypeDto> roles;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.example.bookingappliation.mapper;

import org.example.bookingappliation.config.MapperConfig;
import org.example.bookingappliation.dto.roletypes.response.RoleTypeDto;
import org.example.bookingappliation.model.user.RoleType;
import org.mapstruct.Mapper;

@Mapper(config = MapperConfig.class)
public interface RoleTypeMapper {
RoleTypeDto toDto(RoleType roleType);
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,20 @@
package org.example.bookingappliation.mapper;

import java.util.Set;
import java.util.stream.Collectors;
import org.example.bookingappliation.config.MapperConfig;
import org.example.bookingappliation.dto.users.request.UserRequestDto;
import org.example.bookingappliation.dto.users.request.UserUpdateInfoRequestDto;
import org.example.bookingappliation.dto.users.response.UserResponseDto;
import org.example.bookingappliation.model.user.RoleType;
import org.example.bookingappliation.model.user.User;
import org.mapstruct.AfterMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
import org.mapstruct.NullValuePropertyMappingStrategy;

@Mapper(config = MapperConfig.class,
@Mapper(config = MapperConfig.class, uses = RoleTypeMapper.class,
nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)
public interface UserMapper {
@Mapping(target = "roleNames", ignore = true)
UserResponseDto toResponseDto(User user);

@AfterMapping
default void setRoleNames(@MappingTarget UserResponseDto responseDto, User user) {
Set<String> roleNames = user.getRoles().stream()
.map(RoleType::getName)
.map(Enum::toString)
.collect(Collectors.toSet());
responseDto.setRoleNames(roleNames);
}

@Mapping(target = "password", ignore = true)
@Mapping(target = "roles", ignore = true)
User toModelWithoutPasswordAndRoles(UserRequestDto requestDto);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package org.example.bookingappliation.service;

import org.example.bookingappliation.model.accommodation.AccommodationType;
import java.util.List;
import org.example.bookingappliation.dto.accommodations.response.AccommodationTypeDto;

public interface AccommodationTypeService {
AccommodationType getById(Long id);
List<AccommodationTypeDto> getAll();
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package org.example.bookingappliation.service;

import org.example.bookingappliation.model.accommodation.AmenityType;
import java.util.List;
import org.example.bookingappliation.dto.accommodations.response.AmenityTypeDto;

public interface AmenityTypeService {
AmenityType getById(Long id);
List<AmenityTypeDto> getAll();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.example.bookingappliation.service;

import java.util.List;
import org.example.bookingappliation.dto.bookingstatuses.response.BookingStatusDto;

public interface BookingStatusService {
List<BookingStatusDto> getAll();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.example.bookingappliation.service;

import java.util.List;
import org.example.bookingappliation.dto.paymentstatuses.response.PaymentStatusDto;

public interface PaymentStatusService {
List<PaymentStatusDto> getAll();
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package org.example.bookingappliation.service;

import org.example.bookingappliation.model.accommodation.SizeType;
import java.util.List;
import org.example.bookingappliation.dto.accommodations.response.SizeTypeDto;

public interface SizeTypeService {
SizeType getById(Long id);
List<SizeTypeDto> getAlL();
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.example.bookingappliation.service.impl;

import java.util.Optional;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.example.bookingappliation.dto.accommodations.response.AccommodationTypeDto;
import org.example.bookingappliation.exception.repository.EntityNotFoundException;
import org.example.bookingappliation.mapper.AccommodationTypeMapper;
import org.example.bookingappliation.model.accommodation.AccommodationType;
import org.example.bookingappliation.repository.accommodationtype.AccommodationTypeRepository;
import org.example.bookingappliation.service.AccommodationTypeService;
Expand All @@ -12,13 +14,16 @@
@RequiredArgsConstructor
public class AccommodationTypeServiceImpl implements AccommodationTypeService {
private final AccommodationTypeRepository accommodationTypeRepository;
private final AccommodationTypeMapper accommodationTypeMapper;

@Override
public AccommodationType getById(Long id) {
Optional<AccommodationType> accommodationType = accommodationTypeRepository.findById(id);
if (accommodationType.isEmpty()) {
throw new EntityNotFoundException("Cant find accommodation type with id: " + id);
public List<AccommodationTypeDto> getAll() {
List<AccommodationType> listOfTypes = accommodationTypeRepository.findAll();
if (listOfTypes.isEmpty()) {
throw new EntityNotFoundException("Cant find any accommodation types");
}
return accommodationType.get();
return listOfTypes.stream()
.map(accommodationTypeMapper::toDto)
.toList();
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.example.bookingappliation.service.impl;

import java.util.Optional;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.example.bookingappliation.dto.accommodations.response.AmenityTypeDto;
import org.example.bookingappliation.exception.repository.EntityNotFoundException;
import org.example.bookingappliation.mapper.AmenityTypeMapper;
import org.example.bookingappliation.model.accommodation.AmenityType;
import org.example.bookingappliation.repository.amenitytype.AmenityTypeRepository;
import org.example.bookingappliation.service.AmenityTypeService;
Expand All @@ -12,13 +14,16 @@
@RequiredArgsConstructor
public class AmenityTypeServiceImpl implements AmenityTypeService {
private final AmenityTypeRepository amenityTypeRepository;
private final AmenityTypeMapper amenityTypeMapper;

@Override
public AmenityType getById(Long id) {
Optional<AmenityType> accommodationType = amenityTypeRepository.findById(id);
if (accommodationType.isEmpty()) {
throw new EntityNotFoundException("Cant find amenity type with id: " + id);
public List<AmenityTypeDto> getAll() {
List<AmenityType> listOfTypes = amenityTypeRepository.findAll();
if (listOfTypes.isEmpty()) {
throw new EntityNotFoundException("Cant find any accommodation types");
}
return accommodationType.get();
return listOfTypes.stream()
.map(amenityTypeMapper::toDto)
.toList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.example.bookingappliation.service.impl;

import java.util.List;
import lombok.RequiredArgsConstructor;
import org.example.bookingappliation.dto.bookingstatuses.response.BookingStatusDto;
import org.example.bookingappliation.exception.repository.EntityNotFoundException;
import org.example.bookingappliation.mapper.BookingStatusMapper;
import org.example.bookingappliation.model.booking.BookingStatus;
import org.example.bookingappliation.repository.bookingstatus.BookingStatusRepository;
import org.example.bookingappliation.service.BookingStatusService;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class BookingStatusServiceImpl implements BookingStatusService {
private final BookingStatusRepository bookingStatusRepository;
private final BookingStatusMapper bookingStatusMapper;

@Override
public List<BookingStatusDto> getAll() {
List<BookingStatus> listOfTypes = bookingStatusRepository.findAll();
if (listOfTypes.isEmpty()) {
throw new EntityNotFoundException("Cant find any booking status");
}
return listOfTypes.stream()
.map(bookingStatusMapper::toDto)
.toList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.example.bookingappliation.service.impl;

import java.util.List;
import lombok.RequiredArgsConstructor;
import org.example.bookingappliation.dto.paymentstatuses.response.PaymentStatusDto;
import org.example.bookingappliation.exception.repository.EntityNotFoundException;
import org.example.bookingappliation.mapper.PaymentStatusMapper;
import org.example.bookingappliation.model.payment.PaymentStatus;
import org.example.bookingappliation.repository.paymentstatus.PaymentStatusRepository;
import org.example.bookingappliation.service.PaymentStatusService;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class PaymentStatusServiceImpl implements PaymentStatusService {
private final PaymentStatusRepository paymentStatusRepository;
private final PaymentStatusMapper paymentStatusMapper;

@Override
public List<PaymentStatusDto> getAll() {
List<PaymentStatus> listOfTypes = paymentStatusRepository.findAll();
if (listOfTypes.isEmpty()) {
throw new EntityNotFoundException("Cant find any payment status");
}
return listOfTypes.stream()
.map(paymentStatusMapper::toDto)
.toList();
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.example.bookingappliation.service.impl;

import java.util.Optional;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.example.bookingappliation.dto.accommodations.response.SizeTypeDto;
import org.example.bookingappliation.exception.repository.EntityNotFoundException;
import org.example.bookingappliation.mapper.SizeTypeMapper;
import org.example.bookingappliation.model.accommodation.SizeType;
import org.example.bookingappliation.repository.sizetype.SizeTypeRepository;
import org.example.bookingappliation.service.SizeTypeService;
Expand All @@ -12,13 +14,16 @@
@RequiredArgsConstructor
public class SizeTypeServiceImpl implements SizeTypeService {
private final SizeTypeRepository sizeTypeRepository;
private final SizeTypeMapper sizeTypeMapper;

@Override
public SizeType getById(Long id) {
Optional<SizeType> sizeType = sizeTypeRepository.findById(id);
if (sizeType.isEmpty()) {
throw new EntityNotFoundException("Cant find size type with id: " + id);
public List<SizeTypeDto> getAlL() {
List<SizeType> listOfTypes = sizeTypeRepository.findAll();
if (listOfTypes.isEmpty()) {
throw new EntityNotFoundException("Cant find any size types");
}
return sizeType.get();
return listOfTypes.stream()
.map(sizeTypeMapper::toDto)
.toList();
}
}

0 comments on commit 966e93c

Please sign in to comment.