diff --git a/src/main/java/br/com/beblue/musicstore/ServletInitializer.java b/src/main/java/br/com/beblue/musicstore/ServletInitializer.java deleted file mode 100644 index a1c56d1..0000000 --- a/src/main/java/br/com/beblue/musicstore/ServletInitializer.java +++ /dev/null @@ -1,11 +0,0 @@ -package br.com.beblue.musicstore; - -import org.springframework.boot.builder.SpringApplicationBuilder; -import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; - -public class ServletInitializer extends SpringBootServletInitializer { - @Override - protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { - return builder.sources(MusicStoreApplication.class); - } -} diff --git a/src/main/java/br/com/beblue/musicstore/controller/DiscController.java b/src/main/java/br/com/beblue/musicstore/controller/DiscController.java index eb2aca1..764f0d0 100644 --- a/src/main/java/br/com/beblue/musicstore/controller/DiscController.java +++ b/src/main/java/br/com/beblue/musicstore/controller/DiscController.java @@ -1,47 +1,16 @@ package br.com.beblue.musicstore.controller; -import br.com.beblue.musicstore.dto.DiscDTO; +import br.com.beblue.musicstore.controller.dto.DiscDTO; import br.com.beblue.musicstore.exception.NoValuePresentException; -import br.com.beblue.musicstore.service.DiscService; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; -import org.springframework.http.MediaType; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; -import static br.com.beblue.musicstore.controller.DiscController.ROOT_PATH; +public interface DiscController { -@RestController -@RequestMapping(ROOT_PATH) -public class DiscController { + Page listDiscsByGenre(String genre, Pageable pageable); - static final String ROOT_PATH = "/disc"; - static final String PATH_BY_GENRE = "/genre/{genre}"; - static final String PATH_BY_ID = "/{id}"; + Page listDiscs(Pageable pageable); - private final DiscService discService; - - @Autowired - public DiscController(DiscService discService) { - this.discService = discService; - } - - @GetMapping(value = PATH_BY_GENRE, produces = MediaType.APPLICATION_JSON_VALUE) - public Page list(@PathVariable String genre, Pageable pageable) { - return discService.getDiscs(genre, pageable); - } - - @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE) - public Page list(Pageable pageable) { - return discService.getDiscs(pageable); - } - - @GetMapping(value = PATH_BY_ID, produces = MediaType.APPLICATION_JSON_VALUE) - public DiscDTO get(@PathVariable int id) throws NoValuePresentException { - return discService.getDisc(id); - } + DiscDTO getDiscByID(Integer id) throws NoValuePresentException; } diff --git a/src/main/java/br/com/beblue/musicstore/controller/GenreController.java b/src/main/java/br/com/beblue/musicstore/controller/GenreController.java index 261666f..12633ef 100644 --- a/src/main/java/br/com/beblue/musicstore/controller/GenreController.java +++ b/src/main/java/br/com/beblue/musicstore/controller/GenreController.java @@ -1,32 +1,9 @@ package br.com.beblue.musicstore.controller; -import br.com.beblue.musicstore.dto.GenreDTO; -import br.com.beblue.musicstore.service.GenreService; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.MediaType; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import br.com.beblue.musicstore.controller.dto.GenreDTO; import java.util.List; -import static br.com.beblue.musicstore.controller.GenreController.ROOT_PATH; - -@RestController -@RequestMapping(ROOT_PATH) -public class GenreController { - static final String ROOT_PATH = "/genre"; - - private final GenreService genreService; - - @Autowired - public GenreController(GenreService genreService) { - this.genreService = genreService; - } - - @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE) - public List getAll() { - return genreService.getAllGenres(); - } - +public interface GenreController { + List getAllGenres(); } diff --git a/src/main/java/br/com/beblue/musicstore/controller/HomeController.java b/src/main/java/br/com/beblue/musicstore/controller/HomeController.java deleted file mode 100644 index 3a3e886..0000000 --- a/src/main/java/br/com/beblue/musicstore/controller/HomeController.java +++ /dev/null @@ -1,24 +0,0 @@ -package br.com.beblue.musicstore.controller; - -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -import static br.com.beblue.musicstore.controller.HomeController.ROOT_PATH; - -@RestController -@RequestMapping(ROOT_PATH) -public class HomeController { - static final String ROOT_PATH = "/"; - static final String HOME_MESSAGE = "Welcome to the Music Store API"; - - @GetMapping - public String index() { - return getIndexMessage(); - } - - private String getIndexMessage() { - return HOME_MESSAGE; - } - -} diff --git a/src/main/java/br/com/beblue/musicstore/controller/SaleController.java b/src/main/java/br/com/beblue/musicstore/controller/SaleController.java index 5b1e1ca..f03a2d1 100644 --- a/src/main/java/br/com/beblue/musicstore/controller/SaleController.java +++ b/src/main/java/br/com/beblue/musicstore/controller/SaleController.java @@ -1,56 +1,19 @@ package br.com.beblue.musicstore.controller; -import br.com.beblue.musicstore.dto.SaleRequestDTO; -import br.com.beblue.musicstore.dto.SaleResponseDTO; +import br.com.beblue.musicstore.controller.dto.SaleRequestDTO; +import br.com.beblue.musicstore.controller.dto.SaleResponseDTO; import br.com.beblue.musicstore.exception.IllegalDateException; import br.com.beblue.musicstore.exception.NoValuePresentException; -import br.com.beblue.musicstore.service.SaleSearchService; -import br.com.beblue.musicstore.service.SaleService; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; -import org.springframework.format.annotation.DateTimeFormat; -import org.springframework.web.bind.annotation.*; import java.time.LocalDate; -import static br.com.beblue.musicstore.controller.SaleController.ROOT_PATH; +public interface SaleController { -@RestController -@RequestMapping(ROOT_PATH) -public class SaleController { - static final String ROOT_PATH = "/sale"; - static final String PATH_BY_ORDER_NUMBER = "/{orderNumber}"; - static final String PATH_BY_DATE = "/start_date/{startDate}/end_date/{endDate}"; + SaleResponseDTO registerOrder(SaleRequestDTO request) throws NoValuePresentException; - private final SaleService saleService; - private final SaleSearchService saleSearchService; + SaleResponseDTO getOrderByOrderNumber(String orderNumber) throws NoValuePresentException; - @Autowired - public SaleController(SaleService saleService, SaleSearchService saleSearchService) { - this.saleService = saleService; - this.saleSearchService = saleSearchService; - } - - @PostMapping() - public SaleResponseDTO postOrder(@RequestBody SaleRequestDTO request) throws NoValuePresentException { - return saleService.registerOrder(request); - } - - @GetMapping(value = PATH_BY_ORDER_NUMBER) - public SaleResponseDTO getOrder(@PathVariable String orderNumber) throws NoValuePresentException { - return saleSearchService.getOrderByNumber(orderNumber); - } - - @GetMapping(value = PATH_BY_DATE) - public Page getOrders( - @PathVariable - @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) - LocalDate startDate, - @PathVariable - @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) - LocalDate endDate, - Pageable pageable) throws IllegalDateException { - return saleSearchService.getOrdersByDate(startDate, endDate, pageable); - } + Page getOrdersByDateFilter(LocalDate startDate, LocalDate endDate, Pageable pageable) throws IllegalDateException; } diff --git a/src/main/java/br/com/beblue/musicstore/dto/DiscDTO.java b/src/main/java/br/com/beblue/musicstore/controller/dto/DiscDTO.java similarity index 97% rename from src/main/java/br/com/beblue/musicstore/dto/DiscDTO.java rename to src/main/java/br/com/beblue/musicstore/controller/dto/DiscDTO.java index 520d805..ef9a095 100644 --- a/src/main/java/br/com/beblue/musicstore/dto/DiscDTO.java +++ b/src/main/java/br/com/beblue/musicstore/controller/dto/DiscDTO.java @@ -1,4 +1,4 @@ -package br.com.beblue.musicstore.dto; +package br.com.beblue.musicstore.controller.dto; public class DiscDTO { diff --git a/src/main/java/br/com/beblue/musicstore/dto/GenreDTO.java b/src/main/java/br/com/beblue/musicstore/controller/dto/GenreDTO.java similarity index 90% rename from src/main/java/br/com/beblue/musicstore/dto/GenreDTO.java rename to src/main/java/br/com/beblue/musicstore/controller/dto/GenreDTO.java index 364abe7..a5ecbfc 100644 --- a/src/main/java/br/com/beblue/musicstore/dto/GenreDTO.java +++ b/src/main/java/br/com/beblue/musicstore/controller/dto/GenreDTO.java @@ -1,4 +1,4 @@ -package br.com.beblue.musicstore.dto; +package br.com.beblue.musicstore.controller.dto; public class GenreDTO { private int id; diff --git a/src/main/java/br/com/beblue/musicstore/dto/HttpResponseException.java b/src/main/java/br/com/beblue/musicstore/controller/dto/HttpResponseException.java similarity index 94% rename from src/main/java/br/com/beblue/musicstore/dto/HttpResponseException.java rename to src/main/java/br/com/beblue/musicstore/controller/dto/HttpResponseException.java index 3d6b576..ae125ab 100644 --- a/src/main/java/br/com/beblue/musicstore/dto/HttpResponseException.java +++ b/src/main/java/br/com/beblue/musicstore/controller/dto/HttpResponseException.java @@ -1,4 +1,4 @@ -package br.com.beblue.musicstore.dto; +package br.com.beblue.musicstore.controller.dto; import java.util.ArrayList; import java.util.Date; diff --git a/src/main/java/br/com/beblue/musicstore/dto/SaleRequestDTO.java b/src/main/java/br/com/beblue/musicstore/controller/dto/SaleRequestDTO.java similarity index 90% rename from src/main/java/br/com/beblue/musicstore/dto/SaleRequestDTO.java rename to src/main/java/br/com/beblue/musicstore/controller/dto/SaleRequestDTO.java index 1bf8e62..840eeb0 100644 --- a/src/main/java/br/com/beblue/musicstore/dto/SaleRequestDTO.java +++ b/src/main/java/br/com/beblue/musicstore/controller/dto/SaleRequestDTO.java @@ -1,4 +1,4 @@ -package br.com.beblue.musicstore.dto; +package br.com.beblue.musicstore.controller.dto; import javax.validation.constraints.Min; import java.util.List; diff --git a/src/main/java/br/com/beblue/musicstore/dto/SaleResponseDTO.java b/src/main/java/br/com/beblue/musicstore/controller/dto/SaleResponseDTO.java similarity index 96% rename from src/main/java/br/com/beblue/musicstore/dto/SaleResponseDTO.java rename to src/main/java/br/com/beblue/musicstore/controller/dto/SaleResponseDTO.java index 7d7f056..b86cd82 100644 --- a/src/main/java/br/com/beblue/musicstore/dto/SaleResponseDTO.java +++ b/src/main/java/br/com/beblue/musicstore/controller/dto/SaleResponseDTO.java @@ -1,4 +1,4 @@ -package br.com.beblue.musicstore.dto; +package br.com.beblue.musicstore.controller.dto; import java.util.List; diff --git a/src/main/java/br/com/beblue/musicstore/controller/rest/DiscRestController.java b/src/main/java/br/com/beblue/musicstore/controller/rest/DiscRestController.java new file mode 100644 index 0000000..42e6c8f --- /dev/null +++ b/src/main/java/br/com/beblue/musicstore/controller/rest/DiscRestController.java @@ -0,0 +1,47 @@ +package br.com.beblue.musicstore.controller.rest; + +import br.com.beblue.musicstore.controller.DiscController; +import br.com.beblue.musicstore.controller.dto.DiscDTO; +import br.com.beblue.musicstore.exception.NoValuePresentException; +import br.com.beblue.musicstore.service.DiscService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + + +@RestController +@RequestMapping(DiscRestController.ROOT_PATH) +class DiscRestController implements DiscController { + + static final String ROOT_PATH = "/disc"; + static final String PATH_BY_GENRE = "/genre/{genre}"; + static final String PATH_BY_ID = "/{id}"; + + private final DiscService discService; + + @Autowired + DiscRestController(final DiscService discService) { + this.discService = discService; + } + + @GetMapping(value = PATH_BY_GENRE, produces = MediaType.APPLICATION_JSON_VALUE) + public Page listDiscsByGenre(@PathVariable String genre, Pageable pageable) { + return discService.getDiscsByGenre(genre, pageable); + } + + @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE) + public Page listDiscs(Pageable pageable) { + return discService.getDiscs(pageable); + } + + @GetMapping(value = PATH_BY_ID, produces = MediaType.APPLICATION_JSON_VALUE) + public DiscDTO getDiscByID(@PathVariable Integer id) throws NoValuePresentException { + return discService.getDiscByID(id); + } + +} diff --git a/src/main/java/br/com/beblue/musicstore/controller/rest/GenreRestController.java b/src/main/java/br/com/beblue/musicstore/controller/rest/GenreRestController.java new file mode 100644 index 0000000..637d306 --- /dev/null +++ b/src/main/java/br/com/beblue/musicstore/controller/rest/GenreRestController.java @@ -0,0 +1,32 @@ +package br.com.beblue.musicstore.controller.rest; + +import br.com.beblue.musicstore.controller.GenreController; +import br.com.beblue.musicstore.controller.dto.GenreDTO; +import br.com.beblue.musicstore.service.GenreService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RestController +@RequestMapping(GenreRestController.ROOT_PATH) +class GenreRestController implements GenreController { + + static final String ROOT_PATH = "/genre"; + + private final GenreService genreService; + + @Autowired + GenreRestController(final GenreService genreService) { + this.genreService = genreService; + } + + @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE) + public List getAllGenres() { + return genreService.getAllGenres(); + } + +} diff --git a/src/main/java/br/com/beblue/musicstore/controller/rest/SaleRestController.java b/src/main/java/br/com/beblue/musicstore/controller/rest/SaleRestController.java new file mode 100644 index 0000000..89433b2 --- /dev/null +++ b/src/main/java/br/com/beblue/musicstore/controller/rest/SaleRestController.java @@ -0,0 +1,56 @@ +package br.com.beblue.musicstore.controller.rest; + +import br.com.beblue.musicstore.controller.SaleController; +import br.com.beblue.musicstore.controller.dto.SaleRequestDTO; +import br.com.beblue.musicstore.controller.dto.SaleResponseDTO; +import br.com.beblue.musicstore.exception.IllegalDateException; +import br.com.beblue.musicstore.exception.NoValuePresentException; +import br.com.beblue.musicstore.service.SaleSearchService; +import br.com.beblue.musicstore.service.SaleService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.format.annotation.DateTimeFormat; +import org.springframework.web.bind.annotation.*; + +import java.time.LocalDate; + +@RestController +@RequestMapping(SaleRestController.ROOT_PATH) +class SaleRestController implements SaleController { + + static final String ROOT_PATH = "/sale"; + static final String PATH_BY_ORDER_NUMBER = "/{orderNumber}"; + static final String PATH_BY_DATE = "/start_date/{startDate}/end_date/{endDate}"; + + private final SaleService saleService; + private final SaleSearchService saleSearchService; + + @Autowired + SaleRestController(final SaleService saleService, final SaleSearchService saleSearchService) { + this.saleService = saleService; + this.saleSearchService = saleSearchService; + } + + @PostMapping() + public SaleResponseDTO registerOrder(@RequestBody SaleRequestDTO request) throws NoValuePresentException { + return saleService.registerOrder(request); + } + + @GetMapping(value = PATH_BY_ORDER_NUMBER) + public SaleResponseDTO getOrderByOrderNumber(@PathVariable String orderNumber) throws NoValuePresentException { + return saleSearchService.getOrderByNumber(orderNumber); + } + + @GetMapping(value = PATH_BY_DATE) + public Page getOrdersByDateFilter( + @PathVariable + @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) + LocalDate startDate, + @PathVariable + @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) + LocalDate endDate, + Pageable pageable) throws IllegalDateException { + return saleSearchService.getOrdersByDate(startDate, endDate, pageable); + } +} diff --git a/src/main/java/br/com/beblue/musicstore/events/SaleNotifiable.java b/src/main/java/br/com/beblue/musicstore/events/SaleNotifiable.java new file mode 100644 index 0000000..120cb65 --- /dev/null +++ b/src/main/java/br/com/beblue/musicstore/events/SaleNotifiable.java @@ -0,0 +1,7 @@ +package br.com.beblue.musicstore.events; + +import org.springframework.amqp.AmqpException; + +public interface SaleNotifiable { + void notifyNewOrder(String order) throws AmqpException; +} diff --git a/src/main/java/br/com/beblue/musicstore/amq/SaleAMQSender.java b/src/main/java/br/com/beblue/musicstore/events/impl/SaleAMQNotifiableImpl.java similarity index 66% rename from src/main/java/br/com/beblue/musicstore/amq/SaleAMQSender.java rename to src/main/java/br/com/beblue/musicstore/events/impl/SaleAMQNotifiableImpl.java index 3983360..236c6a9 100644 --- a/src/main/java/br/com/beblue/musicstore/amq/SaleAMQSender.java +++ b/src/main/java/br/com/beblue/musicstore/events/impl/SaleAMQNotifiableImpl.java @@ -1,5 +1,6 @@ -package br.com.beblue.musicstore.amq; +package br.com.beblue.musicstore.events.impl; +import br.com.beblue.musicstore.events.SaleNotifiable; import org.springframework.amqp.AmqpException; import org.springframework.amqp.core.TopicExchange; import org.springframework.amqp.rabbit.core.RabbitTemplate; @@ -8,11 +9,11 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; -import static br.com.beblue.musicstore.util.BeanConst.BEAN_SALE_EXCHANGE; +import static br.com.beblue.musicstore.util.BeansConst.BEAN_SALE_EXCHANGE; import static br.com.beblue.musicstore.util.ResourceConst.KEY_APPLICATION_MQ_SALE_ROUTING_KEY; @Component -public class SaleAMQSender { +class SaleAMQNotifiableImpl implements SaleNotifiable { @Value(KEY_APPLICATION_MQ_SALE_ROUTING_KEY) private String exchangeRoutingKey; @@ -21,12 +22,13 @@ public class SaleAMQSender { private final TopicExchange topicExchange; @Autowired - public SaleAMQSender(RabbitTemplate rabbitTemplate, @Qualifier(BEAN_SALE_EXCHANGE) TopicExchange topicExchange) { + SaleAMQNotifiableImpl(final RabbitTemplate rabbitTemplate, + final @Qualifier(BEAN_SALE_EXCHANGE) TopicExchange topicExchange) { this.rabbitTemplate = rabbitTemplate; this.topicExchange = topicExchange; } - public void send(String order) throws AmqpException { + public void notifyNewOrder(String order) throws AmqpException { rabbitTemplate.convertAndSend(topicExchange.getName(), exchangeRoutingKey, order); } diff --git a/src/main/java/br/com/beblue/musicstore/exception/AlreadyImportedDiscsException.java b/src/main/java/br/com/beblue/musicstore/exception/AlreadyImportedDiscsException.java index 0b08a05..3550d6c 100644 --- a/src/main/java/br/com/beblue/musicstore/exception/AlreadyImportedDiscsException.java +++ b/src/main/java/br/com/beblue/musicstore/exception/AlreadyImportedDiscsException.java @@ -1,12 +1,18 @@ package br.com.beblue.musicstore.exception; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + public class AlreadyImportedDiscsException extends ImportedException { + + private static final Logger LOGGER = LoggerFactory.getLogger(AlreadyImportedDiscsException.class.getName()); + public AlreadyImportedDiscsException() { super("Discs already imported"); } @Override public void printStackTrace() { - System.out.println("[INFO] "+getMessage()); + LOGGER.info(getMessage()); } } diff --git a/src/main/java/br/com/beblue/musicstore/exception/ImportedException.java b/src/main/java/br/com/beblue/musicstore/exception/ImportedException.java index 9cf1bfe..0377118 100644 --- a/src/main/java/br/com/beblue/musicstore/exception/ImportedException.java +++ b/src/main/java/br/com/beblue/musicstore/exception/ImportedException.java @@ -1,22 +1,10 @@ package br.com.beblue.musicstore.exception; public class ImportedException extends Exception { - public ImportedException() { - } - public ImportedException(String message) { super(message); } - - public ImportedException(String message, Throwable cause) { - super(message, cause); - } - public ImportedException(Throwable cause) { super(cause); } - - public ImportedException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { - super(message, cause, enableSuppression, writableStackTrace); - } } diff --git a/src/main/java/br/com/beblue/musicstore/model/repository/SpotifyRepository.java b/src/main/java/br/com/beblue/musicstore/model/repository/SpotifyRepository.java index 422533c..b99cfb6 100644 --- a/src/main/java/br/com/beblue/musicstore/model/repository/SpotifyRepository.java +++ b/src/main/java/br/com/beblue/musicstore/model/repository/SpotifyRepository.java @@ -1,25 +1,11 @@ package br.com.beblue.musicstore.model.repository; -import br.com.beblue.musicstore.spotify.SpotifyAPI; import com.wrapper.spotify.exceptions.SpotifyWebApiException; import com.wrapper.spotify.model_objects.specification.Paging; import com.wrapper.spotify.model_objects.specification.Track; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Repository; import java.io.IOException; -@Repository -public class SpotifyRepository { - - private final SpotifyAPI spotifyAPI; - - @Autowired - public SpotifyRepository(SpotifyAPI spotifyAPI) { - this.spotifyAPI = spotifyAPI; - } - - public Paging findTrackByGenre(String genre) throws IOException, SpotifyWebApiException { - return spotifyAPI.findTrackByGenre(genre); - } +public interface SpotifyRepository { + Paging findTrackByGenre(String genre) throws IOException, SpotifyWebApiException; } diff --git a/src/main/java/br/com/beblue/musicstore/model/repository/impl/SpotifyRepositoryImpl.java b/src/main/java/br/com/beblue/musicstore/model/repository/impl/SpotifyRepositoryImpl.java new file mode 100644 index 0000000..f9b41a0 --- /dev/null +++ b/src/main/java/br/com/beblue/musicstore/model/repository/impl/SpotifyRepositoryImpl.java @@ -0,0 +1,26 @@ +package br.com.beblue.musicstore.model.repository.impl; + +import br.com.beblue.musicstore.model.repository.SpotifyRepository; +import br.com.beblue.musicstore.spotify.SpotifyAPI; +import com.wrapper.spotify.exceptions.SpotifyWebApiException; +import com.wrapper.spotify.model_objects.specification.Paging; +import com.wrapper.spotify.model_objects.specification.Track; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Repository; + +import java.io.IOException; + +@Repository +class SpotifyRepositoryImpl implements SpotifyRepository { + + private final SpotifyAPI spotifyAPI; + + @Autowired + SpotifyRepositoryImpl(SpotifyAPI spotifyAPI) { + this.spotifyAPI = spotifyAPI; + } + + public Paging findTrackByGenre(String genre) throws IOException, SpotifyWebApiException { + return spotifyAPI.findTrackByGenre(genre); + } +} diff --git a/src/main/java/br/com/beblue/musicstore/service/CashbackService.java b/src/main/java/br/com/beblue/musicstore/service/CashbackService.java index 1c3ed60..f7655cc 100644 --- a/src/main/java/br/com/beblue/musicstore/service/CashbackService.java +++ b/src/main/java/br/com/beblue/musicstore/service/CashbackService.java @@ -1,30 +1,9 @@ package br.com.beblue.musicstore.service; import br.com.beblue.musicstore.model.entity.GenreCashbackEntity; -import br.com.beblue.musicstore.model.repository.GenreCashbackRepository; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; import java.util.List; -import static br.com.beblue.musicstore.util.DateUtil.getDayOfWeek; - -@Service -public class CashbackService { - - private final GenreCashbackRepository cashbackRepository; - - @Autowired - public CashbackService(GenreCashbackRepository cashbackRepository) { - this.cashbackRepository = cashbackRepository; - } - - public GenreCashbackEntity getCashback(Integer genreId) { - return cashbackRepository.findByGenreEntityIdAndWeekdayAndEnableTrue(genreId, getDayOfWeek()); - } - - public List getCashbacks() { - return cashbackRepository.findByWeekdayAndEnableTrue(getDayOfWeek()); - } - -} \ No newline at end of file +public interface CashbackService { + List getCashbacks(); +} diff --git a/src/main/java/br/com/beblue/musicstore/service/DiscService.java b/src/main/java/br/com/beblue/musicstore/service/DiscService.java index 3468003..5ac413d 100644 --- a/src/main/java/br/com/beblue/musicstore/service/DiscService.java +++ b/src/main/java/br/com/beblue/musicstore/service/DiscService.java @@ -1,65 +1,14 @@ package br.com.beblue.musicstore.service; -import br.com.beblue.musicstore.dto.DiscDTO; +import br.com.beblue.musicstore.controller.dto.DiscDTO; import br.com.beblue.musicstore.exception.NoValuePresentException; -import br.com.beblue.musicstore.model.entity.DiscEntity; -import br.com.beblue.musicstore.model.entity.GenreCashbackEntity; -import br.com.beblue.musicstore.model.repository.DiscRepository; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; -import org.springframework.stereotype.Service; -import java.util.Optional; +public interface DiscService { + Page getDiscsByGenre(String genre, Pageable pageable); -import static br.com.beblue.musicstore.util.converter.DiscConverter.discEntityToDiscDTO; -import static br.com.beblue.musicstore.util.converter.DiscConverter.pageEntityToPageDTO; - -@Service -public class DiscService { - - private final DiscRepository discRepository; - private final CashbackService cashbackService; - - @Autowired - public DiscService(DiscRepository discRepository, CashbackService cashbackService) { - this.discRepository = discRepository; - this.cashbackService = cashbackService; - } - - public Page getDiscs(Pageable pageable) { - Page entities = discRepository.findAllByOrderByName(pageable); - return fillCashbackAndConvert(entities); - } - - public Page getDiscs(String genre, Pageable pageable) { - Page entities = discRepository.findAllByGenreEntityNameIgnoreCaseOrderByName(genre, pageable); - return fillCashbackAndConvert(entities); - } - - public DiscDTO getDisc(int id) throws NoValuePresentException { - Optional byId = discRepository.findById(id); - if (byId.isPresent()) { - DiscDTO discDTO = discEntityToDiscDTO(byId.get()); - return fillCashback(discDTO); - } - throw new NoValuePresentException("No disc with id=" + id); - } - - private Page fillCashbackAndConvert(Page entities) { - Page discDTOS = pageEntityToPageDTO(entities); - return fillCashback(discDTOS); - } - - private Page fillCashback(Page entities) { - entities.forEach(this::fillCashback); - return entities; - } - - private DiscDTO fillCashback(DiscDTO disc) { - GenreCashbackEntity cashback = cashbackService.getCashback(disc.getGenre().getId()); - disc.setCashback(cashback.getCashback()); - return disc; - } + Page getDiscs(Pageable pageable); + DiscDTO getDiscByID(Integer id) throws NoValuePresentException; } diff --git a/src/main/java/br/com/beblue/musicstore/service/GenreService.java b/src/main/java/br/com/beblue/musicstore/service/GenreService.java index 23540d4..7dcebb0 100644 --- a/src/main/java/br/com/beblue/musicstore/service/GenreService.java +++ b/src/main/java/br/com/beblue/musicstore/service/GenreService.java @@ -1,27 +1,9 @@ package br.com.beblue.musicstore.service; -import br.com.beblue.musicstore.dto.GenreDTO; -import br.com.beblue.musicstore.model.repository.GenreRepository; -import br.com.beblue.musicstore.util.converter.GenreConverter; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; +import br.com.beblue.musicstore.controller.dto.GenreDTO; -import java.util.ArrayList; import java.util.List; -@Service -public class GenreService { - - private final GenreRepository genreRepository; - - @Autowired - public GenreService(GenreRepository genreRepository) { - this.genreRepository = genreRepository; - } - - public List getAllGenres() { - List list = new ArrayList<>(); - genreRepository.findAll().forEach(genreEntity -> list.add(GenreConverter.genreEntityToGenreDTO(genreEntity))); - return list; - } +public interface GenreService { + List getAllGenres(); } diff --git a/src/main/java/br/com/beblue/musicstore/service/SaleSearchService.java b/src/main/java/br/com/beblue/musicstore/service/SaleSearchService.java index d2075cd..8c4f500 100644 --- a/src/main/java/br/com/beblue/musicstore/service/SaleSearchService.java +++ b/src/main/java/br/com/beblue/musicstore/service/SaleSearchService.java @@ -1,47 +1,15 @@ package br.com.beblue.musicstore.service; -import br.com.beblue.musicstore.dto.SaleResponseDTO; +import br.com.beblue.musicstore.controller.dto.SaleResponseDTO; import br.com.beblue.musicstore.exception.IllegalDateException; import br.com.beblue.musicstore.exception.NoValuePresentException; -import br.com.beblue.musicstore.model.entity.SaleEntity; -import br.com.beblue.musicstore.model.repository.SaleRepository; -import br.com.beblue.musicstore.util.converter.SaleConverter; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; -import org.springframework.stereotype.Service; -import java.sql.Date; import java.time.LocalDate; -import java.util.Optional; -@Service -public class SaleSearchService { +public interface SaleSearchService { + SaleResponseDTO getOrderByNumber(String orderNumber) throws NoValuePresentException; - private final SaleRepository saleRepository; - - @Autowired - public SaleSearchService(SaleRepository saleRepository) { - this.saleRepository = saleRepository; - } - - public SaleResponseDTO getOrderByNumber(String orderNumber) throws NoValuePresentException { - Optional optional = saleRepository.findByUuid(orderNumber); - if (!optional.isPresent()) throw new NoValuePresentException("No sale with order number: " + orderNumber); - SaleEntity saleEntity = optional.get(); - return SaleConverter.saleEntityToSaleResponseDTO(saleEntity); - } - - public Page getOrdersByDate(LocalDate startDate, LocalDate endDate, Pageable pageable) throws IllegalDateException { - checkDates(startDate, endDate); - Page entities = saleRepository.findAllBySaleDateTimeBetween(Date.valueOf(startDate), Date.valueOf(endDate), pageable); - return SaleConverter.saleListEntityToSaleResponseList(entities); - } - - private void checkDates(LocalDate startDate, LocalDate endDate) throws IllegalDateException { - if (startDate.isAfter(endDate)) - throw new IllegalDateException("start date can't be greater than end date"); - if (endDate.isAfter(LocalDate.now())) - throw new IllegalDateException("end date can't be greater than current date"); - } + Page getOrdersByDate(LocalDate startDate, LocalDate endDate, Pageable pageable) throws IllegalDateException; } diff --git a/src/main/java/br/com/beblue/musicstore/service/SaleService.java b/src/main/java/br/com/beblue/musicstore/service/SaleService.java index 00cd4ee..bdc5c43 100644 --- a/src/main/java/br/com/beblue/musicstore/service/SaleService.java +++ b/src/main/java/br/com/beblue/musicstore/service/SaleService.java @@ -1,114 +1,9 @@ package br.com.beblue.musicstore.service; -import br.com.beblue.musicstore.amq.SaleAMQSender; -import br.com.beblue.musicstore.dto.SaleRequestDTO; -import br.com.beblue.musicstore.dto.SaleResponseDTO; +import br.com.beblue.musicstore.controller.dto.SaleRequestDTO; +import br.com.beblue.musicstore.controller.dto.SaleResponseDTO; import br.com.beblue.musicstore.exception.NoValuePresentException; -import br.com.beblue.musicstore.model.entity.DiscEntity; -import br.com.beblue.musicstore.model.entity.DiscSaleEntity; -import br.com.beblue.musicstore.model.entity.GenreCashbackEntity; -import br.com.beblue.musicstore.model.entity.SaleEntity; -import br.com.beblue.musicstore.model.repository.DiscRepository; -import br.com.beblue.musicstore.model.repository.SaleRepository; -import br.com.beblue.musicstore.util.converter.SaleConverter; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; -import java.util.*; - -@Service -public class SaleService { - - private final SaleRepository saleRepository; - private final DiscRepository discRepository; - private final CashbackService cashbackService; - private final SaleAMQSender saleAMQSender; - - @Autowired - public SaleService(SaleRepository saleRepository, DiscRepository discRepository, CashbackService cashbackService, SaleAMQSender saleAMQSender) { - this.saleRepository = saleRepository; - this.discRepository = discRepository; - this.cashbackService = cashbackService; - this.saleAMQSender = saleAMQSender; - } - - public SaleResponseDTO registerOrder(SaleRequestDTO item) throws NoValuePresentException { - - List entities = validateAndGetEntities(item); - Map cashbackMap = genreCashbackEntitiesToMap(cashbackService.getCashbacks()); - - SaleEntity saleEntity = new SaleEntity(); - saleEntity.setUuid(UUID.randomUUID().toString()); - - entities.forEach(entity -> { - Integer cashback = cashbackMap.get(entity.getGenreEntity().getId()); - double cashbackPrice = getCashbackPrice(entity.getPrice(), cashback); - DiscSaleEntity discSaleEntity = new DiscSaleEntity(); - discSaleEntity.setSaleEntity(saleEntity); - discSaleEntity.setCashback(cashback); - discSaleEntity.setPrice(entity.getPrice()); - discSaleEntity.setDiscEntity(entity); - discSaleEntity.setPrice_cashback(cashbackPrice); - saleEntity.setTotal_price(saleEntity.getTotal_price() + entity.getPrice()); - saleEntity.setCashback_price(saleEntity.getCashback_price() + cashbackPrice); - saleEntity.addDiscSaleEntity(discSaleEntity); - }); - - persist(saleEntity); - notifyMessageQueue(saleEntity); - - return SaleConverter.saleEntityToSaleResponseDTO(saleEntity); - } - - private void notifyMessageQueue(SaleEntity saleEntity) { - // Caso falte comunicação ou de erro de persistencia, o sistema não - //deve parar. Posteriormente tratar o Log de erro - try { - saleAMQSender.send(saleEntity.getUuid()); - } catch (Throwable t) { - t.printStackTrace(); - } - } - - private void persist(SaleEntity saleEntity) { - saleRepository.save(saleEntity); - } - - private double getCashbackPrice(double price, Integer cashback) { - return (price * cashback) / 100; - } - - private Map genreCashbackEntitiesToMap(List cashbackList) { - Map map = new HashMap<>(); - cashbackList.forEach(item -> map.put(item.getGenreEntity().getId(), item.getCashback())); - return map; - } - - private List validateAndGetEntities(SaleRequestDTO items) throws NoValuePresentException { - checkInput(items); - List discEntities = findDiscs(items); - checkNoPresentIds(items, discEntities); - return discEntities; - } - - private void checkInput(SaleRequestDTO request) throws NoValuePresentException { - if (request.getDiscsIds() == null || request.getDiscsIds().isEmpty()) - throw new NoValuePresentException("Empty discs id"); - } - - private List findDiscs(SaleRequestDTO request) { - return discRepository.findByIdIn(request.getDiscsIds()); - } - - private void checkNoPresentIds(SaleRequestDTO items, List discEntities) throws NoValuePresentException { - if (items.getDiscsIds().size() == discEntities.size()) return; - List noPresentValues = new ArrayList<>(); - discEntities.forEach(discEntity -> { - List l = items.getDiscsIds(); - if (l.stream().anyMatch(integer -> integer == discEntity.getId())) { - noPresentValues.add(discEntity.getId()); - } - }); - throw new NoValuePresentException("Discs not available: " + noPresentValues.toString()); - } +public interface SaleService { + SaleResponseDTO registerOrder(SaleRequestDTO request) throws NoValuePresentException; } diff --git a/src/main/java/br/com/beblue/musicstore/service/SpotifyImportService.java b/src/main/java/br/com/beblue/musicstore/service/SpotifyImportService.java index fb19ecf..f4b2f52 100644 --- a/src/main/java/br/com/beblue/musicstore/service/SpotifyImportService.java +++ b/src/main/java/br/com/beblue/musicstore/service/SpotifyImportService.java @@ -1,115 +1,13 @@ package br.com.beblue.musicstore.service; -import br.com.beblue.musicstore.exception.AlreadyImportedDiscsException; -import br.com.beblue.musicstore.exception.NoGenresException; import br.com.beblue.musicstore.exception.ImportedException; -import br.com.beblue.musicstore.model.entity.DiscEntity; -import br.com.beblue.musicstore.model.entity.GenreEntity; -import br.com.beblue.musicstore.model.repository.DiscRepository; -import br.com.beblue.musicstore.model.repository.GenreRepository; -import br.com.beblue.musicstore.model.repository.SpotifyRepository; -import br.com.beblue.musicstore.util.PriceUtil; -import com.wrapper.spotify.exceptions.SpotifyWebApiException; -import com.wrapper.spotify.model_objects.specification.Track; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; -import java.io.IOException; -import java.util.Arrays; -import java.util.List; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.TimeUnit; - -import static br.com.beblue.musicstore.util.converter.DiscConverter.trackToDiscEntity; - -@Service -public class SpotifyImportService { +public interface SpotifyImportService { @FunctionalInterface - public interface ImportErrorCallback { + interface ImportErrorCallback { void error(ImportedException throwable); } - private final GenreRepository genreRepository; - private final DiscRepository discRepository; - private final SpotifyRepository spotifyRepository; - - @Autowired - public SpotifyImportService(DiscRepository discRepository, - SpotifyRepository spotifyRepository, - GenreRepository genreRepository) { - this.genreRepository = genreRepository; - this.discRepository = discRepository; - this.spotifyRepository = spotifyRepository; - } - - - public void importDiscsByGenres(ImportErrorCallback importErrorCallback) { - if (checkAlreadyImported(importErrorCallback)) return; - try { - executeImportDiscs(importErrorCallback); - } catch (NoGenresException e) { - importErrorCallback.error(e); - } - } - - private boolean checkAlreadyImported(ImportErrorCallback importErrorCallback) { - // Poderia ser realizado uma consistencia mais adequada para verificar se os discos já foram importados. - if (discRepository.count() > 0) { - importErrorCallback.error(new AlreadyImportedDiscsException()); - return true; - } - return false; - } - - private void executeImportDiscs(ImportErrorCallback importErrorCallback) throws NoGenresException { - ExecutorService executorService = createExecutorToImportDiscs(); - genreRepository.findAll().forEach(genre -> executorService.submit(() -> { - try { - importGenre(genre); - } catch (IOException | SpotifyWebApiException e) { - importErrorCallback.error(new ImportedException(e)); - } - })); - executorService.shutdown(); - awaitImport(importErrorCallback, executorService); - } - - private ExecutorService createExecutorToImportDiscs() throws NoGenresException { - int value = Long.valueOf(genreRepository.count()).intValue(); - if (value > 0) { - return Executors.newFixedThreadPool(value); - } else { - throw new NoGenresException(); - } - } - - private void importGenre(GenreEntity genre) throws IOException, SpotifyWebApiException { - getFirstsItemsByGenre(genre).parallelStream().forEach(track -> saveDisc(track, genre)); - } - - private List getFirstsItemsByGenre(GenreEntity genre) throws IOException, SpotifyWebApiException { - return Arrays.asList(spotifyRepository.findTrackByGenre(genre.getName()).getItems()); - } - - private void saveDisc(Track track, GenreEntity genreEntity) { - DiscEntity discEntity = convertToDisc(track, genreEntity); - discRepository.save(discEntity); - } - - private DiscEntity convertToDisc(Track track, GenreEntity genreEntity) { - DiscEntity discEntity = trackToDiscEntity(track); - discEntity.setGenreEntity(genreEntity); - discEntity.setPrice(PriceUtil.generateRandomPriceDisc()); - return discEntity; - } - - private void awaitImport(ImportErrorCallback importErrorCallback, ExecutorService executorService) { - try { - executorService.awaitTermination(2, TimeUnit.MINUTES); - } catch (InterruptedException in) { - importErrorCallback.error(new ImportedException(in)); - } - } + void importDiscsByGenres(SpotifyImportService.ImportErrorCallback importErrorCallback); } diff --git a/src/main/java/br/com/beblue/musicstore/service/impl/CashbackServiceImpl.java b/src/main/java/br/com/beblue/musicstore/service/impl/CashbackServiceImpl.java new file mode 100644 index 0000000..d87e2c4 --- /dev/null +++ b/src/main/java/br/com/beblue/musicstore/service/impl/CashbackServiceImpl.java @@ -0,0 +1,31 @@ +package br.com.beblue.musicstore.service.impl; + +import br.com.beblue.musicstore.model.entity.GenreCashbackEntity; +import br.com.beblue.musicstore.model.repository.GenreCashbackRepository; +import br.com.beblue.musicstore.service.CashbackService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +import static br.com.beblue.musicstore.util.DateUtil.getDayOfWeek; + +@Service +class CashbackServiceImpl implements CashbackService { + + private final GenreCashbackRepository cashbackRepository; + + @Autowired + CashbackServiceImpl(final GenreCashbackRepository cashbackRepository) { + this.cashbackRepository = cashbackRepository; + } + + public GenreCashbackEntity getCashback(Integer genreId) { + return cashbackRepository.findByGenreEntityIdAndWeekdayAndEnableTrue(genreId, getDayOfWeek()); + } + + public List getCashbacks() { + return cashbackRepository.findByWeekdayAndEnableTrue(getDayOfWeek()); + } + +} \ No newline at end of file diff --git a/src/main/java/br/com/beblue/musicstore/service/impl/DiscServiceImpl.java b/src/main/java/br/com/beblue/musicstore/service/impl/DiscServiceImpl.java new file mode 100644 index 0000000..e084e09 --- /dev/null +++ b/src/main/java/br/com/beblue/musicstore/service/impl/DiscServiceImpl.java @@ -0,0 +1,66 @@ +package br.com.beblue.musicstore.service.impl; + +import br.com.beblue.musicstore.controller.dto.DiscDTO; +import br.com.beblue.musicstore.exception.NoValuePresentException; +import br.com.beblue.musicstore.model.entity.DiscEntity; +import br.com.beblue.musicstore.model.entity.GenreCashbackEntity; +import br.com.beblue.musicstore.model.repository.DiscRepository; +import br.com.beblue.musicstore.service.DiscService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.stereotype.Service; + +import java.util.Optional; + +import static br.com.beblue.musicstore.util.mapper.DiscMapper.discEntityToDiscDTO; +import static br.com.beblue.musicstore.util.mapper.DiscMapper.pageEntityToPageDTO; + +@Service +class DiscServiceImpl implements DiscService { + + private final DiscRepository discRepository; + private final CashbackServiceImpl cashbackService; + + @Autowired + DiscServiceImpl(final DiscRepository discRepository, final CashbackServiceImpl cashbackService) { + this.discRepository = discRepository; + this.cashbackService = cashbackService; + } + + public Page getDiscs(Pageable pageable) { + Page entities = discRepository.findAllByOrderByName(pageable); + return fillCashbackAndConvert(entities); + } + + public Page getDiscsByGenre(String genre, Pageable pageable) { + Page entities = discRepository.findAllByGenreEntityNameIgnoreCaseOrderByName(genre, pageable); + return fillCashbackAndConvert(entities); + } + + public DiscDTO getDiscByID(Integer id) throws NoValuePresentException { + Optional byId = discRepository.findById(id); + if (byId.isPresent()) { + DiscDTO discDTO = discEntityToDiscDTO(byId.get()); + return fillCashback(discDTO); + } + throw new NoValuePresentException("No disc with id=" + id); + } + + private Page fillCashbackAndConvert(Page entities) { + Page discDTOS = pageEntityToPageDTO(entities); + return fillCashback(discDTOS); + } + + private Page fillCashback(Page entities) { + entities.forEach(this::fillCashback); + return entities; + } + + private DiscDTO fillCashback(DiscDTO disc) { + GenreCashbackEntity cashback = cashbackService.getCashback(disc.getGenre().getId()); + disc.setCashback(cashback.getCashback()); + return disc; + } + +} diff --git a/src/main/java/br/com/beblue/musicstore/service/impl/GenreServiceImpl.java b/src/main/java/br/com/beblue/musicstore/service/impl/GenreServiceImpl.java new file mode 100644 index 0000000..09179f9 --- /dev/null +++ b/src/main/java/br/com/beblue/musicstore/service/impl/GenreServiceImpl.java @@ -0,0 +1,28 @@ +package br.com.beblue.musicstore.service.impl; + +import br.com.beblue.musicstore.controller.dto.GenreDTO; +import br.com.beblue.musicstore.model.repository.GenreRepository; +import br.com.beblue.musicstore.service.GenreService; +import br.com.beblue.musicstore.util.mapper.GenreMapper; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; + +@Service +class GenreServiceImpl implements GenreService { + + private final GenreRepository genreRepository; + + @Autowired + GenreServiceImpl(final GenreRepository genreRepository) { + this.genreRepository = genreRepository; + } + + public List getAllGenres() { + List list = new ArrayList<>(); + genreRepository.findAll().forEach(genreEntity -> list.add(GenreMapper.genreEntityToGenreDTO(genreEntity))); + return list; + } +} diff --git a/src/main/java/br/com/beblue/musicstore/service/impl/SaleSearchServiceImpl.java b/src/main/java/br/com/beblue/musicstore/service/impl/SaleSearchServiceImpl.java new file mode 100644 index 0000000..0753af8 --- /dev/null +++ b/src/main/java/br/com/beblue/musicstore/service/impl/SaleSearchServiceImpl.java @@ -0,0 +1,48 @@ +package br.com.beblue.musicstore.service.impl; + +import br.com.beblue.musicstore.controller.dto.SaleResponseDTO; +import br.com.beblue.musicstore.exception.IllegalDateException; +import br.com.beblue.musicstore.exception.NoValuePresentException; +import br.com.beblue.musicstore.model.entity.SaleEntity; +import br.com.beblue.musicstore.model.repository.SaleRepository; +import br.com.beblue.musicstore.service.SaleSearchService; +import br.com.beblue.musicstore.util.mapper.SaleMapper; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.stereotype.Service; + +import java.sql.Date; +import java.time.LocalDate; +import java.util.Optional; + +@Service +class SaleSearchServiceImpl implements SaleSearchService { + + private final SaleRepository saleRepository; + + @Autowired + SaleSearchServiceImpl(final SaleRepository saleRepository) { + this.saleRepository = saleRepository; + } + + public SaleResponseDTO getOrderByNumber(String orderNumber) throws NoValuePresentException { + Optional optional = saleRepository.findByUuid(orderNumber); + if (!optional.isPresent()) throw new NoValuePresentException("No sale with order number: " + orderNumber); + SaleEntity saleEntity = optional.get(); + return SaleMapper.saleEntityToSaleResponseDTO(saleEntity); + } + + public Page getOrdersByDate(LocalDate startDate, LocalDate endDate, Pageable pageable) throws IllegalDateException { + checkDates(startDate, endDate); + Page entities = saleRepository.findAllBySaleDateTimeBetween(Date.valueOf(startDate), Date.valueOf(endDate), pageable); + return SaleMapper.saleListEntityToSaleResponseList(entities); + } + + private void checkDates(LocalDate startDate, LocalDate endDate) throws IllegalDateException { + if (startDate.isAfter(endDate)) + throw new IllegalDateException("start date can't be greater than end date"); + if (endDate.isAfter(LocalDate.now())) + throw new IllegalDateException("end date can't be greater than current date"); + } +} diff --git a/src/main/java/br/com/beblue/musicstore/service/impl/SaleServiceImpl.java b/src/main/java/br/com/beblue/musicstore/service/impl/SaleServiceImpl.java new file mode 100644 index 0000000..ad0e3c3 --- /dev/null +++ b/src/main/java/br/com/beblue/musicstore/service/impl/SaleServiceImpl.java @@ -0,0 +1,119 @@ +package br.com.beblue.musicstore.service.impl; + +import br.com.beblue.musicstore.events.SaleNotifiable; +import br.com.beblue.musicstore.controller.dto.SaleRequestDTO; +import br.com.beblue.musicstore.controller.dto.SaleResponseDTO; +import br.com.beblue.musicstore.exception.NoValuePresentException; +import br.com.beblue.musicstore.model.entity.DiscEntity; +import br.com.beblue.musicstore.model.entity.DiscSaleEntity; +import br.com.beblue.musicstore.model.entity.GenreCashbackEntity; +import br.com.beblue.musicstore.model.entity.SaleEntity; +import br.com.beblue.musicstore.model.repository.DiscRepository; +import br.com.beblue.musicstore.model.repository.SaleRepository; +import br.com.beblue.musicstore.service.CashbackService; +import br.com.beblue.musicstore.service.SaleService; +import br.com.beblue.musicstore.util.mapper.SaleMapper; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.*; + +@Service +class SaleServiceImpl implements SaleService { + + private final SaleRepository saleRepository; + private final DiscRepository discRepository; + private final CashbackService cashbackService; + private final SaleNotifiable saleNotifiable; + + @Autowired + SaleServiceImpl(final SaleRepository saleRepository, + final DiscRepository discRepository, + final CashbackService cashbackService, + final SaleNotifiable saleNotifiable) { + this.saleRepository = saleRepository; + this.discRepository = discRepository; + this.cashbackService = cashbackService; + this.saleNotifiable = saleNotifiable; + } + + public SaleResponseDTO registerOrder(SaleRequestDTO item) throws NoValuePresentException { + + List entities = validateAndGetEntities(item); + Map cashbackMap = genreCashbackEntitiesToMap(cashbackService.getCashbacks()); + + SaleEntity saleEntity = new SaleEntity(); + saleEntity.setUuid(UUID.randomUUID().toString()); + + entities.forEach(entity -> { + Integer cashback = cashbackMap.get(entity.getGenreEntity().getId()); + double cashbackPrice = getCashbackPrice(entity.getPrice(), cashback); + DiscSaleEntity discSaleEntity = new DiscSaleEntity(); + discSaleEntity.setSaleEntity(saleEntity); + discSaleEntity.setCashback(cashback); + discSaleEntity.setPrice(entity.getPrice()); + discSaleEntity.setDiscEntity(entity); + discSaleEntity.setPrice_cashback(cashbackPrice); + saleEntity.setTotal_price(saleEntity.getTotal_price() + entity.getPrice()); + saleEntity.setCashback_price(saleEntity.getCashback_price() + cashbackPrice); + saleEntity.addDiscSaleEntity(discSaleEntity); + }); + + persist(saleEntity); + notifyMessageQueue(saleEntity); + + return SaleMapper.saleEntityToSaleResponseDTO(saleEntity); + } + + private void notifyMessageQueue(SaleEntity saleEntity) { + // Caso falte comunicação ou de erro de persistencia, o sistema não + //deve parar. Posteriormente tratar o Log de erro + try { + saleNotifiable.notifyNewOrder(saleEntity.getUuid()); + } catch (Throwable t) { + t.printStackTrace(); + } + } + + private void persist(SaleEntity saleEntity) { + saleRepository.save(saleEntity); + } + + private double getCashbackPrice(double price, Integer cashback) { + return (price * cashback) / 100; + } + + private Map genreCashbackEntitiesToMap(List cashbackList) { + Map map = new HashMap<>(); + cashbackList.forEach(item -> map.put(item.getGenreEntity().getId(), item.getCashback())); + return map; + } + + private List validateAndGetEntities(SaleRequestDTO items) throws NoValuePresentException { + checkInput(items); + List discEntities = findDiscs(items); + checkNoPresentIds(items, discEntities); + return discEntities; + } + + private void checkInput(SaleRequestDTO request) throws NoValuePresentException { + if (request.getDiscsIds() == null || request.getDiscsIds().isEmpty()) + throw new NoValuePresentException("Empty discs id"); + } + + private List findDiscs(SaleRequestDTO request) { + return discRepository.findByIdIn(request.getDiscsIds()); + } + + private void checkNoPresentIds(SaleRequestDTO items, List discEntities) throws NoValuePresentException { + if (items.getDiscsIds().size() == discEntities.size()) return; + List noPresentValues = new ArrayList<>(); + discEntities.forEach(discEntity -> { + List l = items.getDiscsIds(); + if (l.stream().anyMatch(integer -> integer == discEntity.getId())) { + noPresentValues.add(discEntity.getId()); + } + }); + throw new NoValuePresentException("Discs not available: " + noPresentValues.toString()); + } +} diff --git a/src/main/java/br/com/beblue/musicstore/service/impl/SpotifyImportServiceImpl.java b/src/main/java/br/com/beblue/musicstore/service/impl/SpotifyImportServiceImpl.java new file mode 100644 index 0000000..6aae523 --- /dev/null +++ b/src/main/java/br/com/beblue/musicstore/service/impl/SpotifyImportServiceImpl.java @@ -0,0 +1,111 @@ +package br.com.beblue.musicstore.service.impl; + +import br.com.beblue.musicstore.exception.AlreadyImportedDiscsException; +import br.com.beblue.musicstore.exception.ImportedException; +import br.com.beblue.musicstore.exception.NoGenresException; +import br.com.beblue.musicstore.model.entity.DiscEntity; +import br.com.beblue.musicstore.model.entity.GenreEntity; +import br.com.beblue.musicstore.model.repository.DiscRepository; +import br.com.beblue.musicstore.model.repository.GenreRepository; +import br.com.beblue.musicstore.model.repository.SpotifyRepository; +import br.com.beblue.musicstore.service.SpotifyImportService; +import br.com.beblue.musicstore.util.PriceUtil; +import com.wrapper.spotify.exceptions.SpotifyWebApiException; +import com.wrapper.spotify.model_objects.specification.Track; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.io.IOException; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; + +import static br.com.beblue.musicstore.util.mapper.DiscMapper.trackToDiscEntity; + +@Service +class SpotifyImportServiceImpl implements SpotifyImportService { + + private final GenreRepository genreRepository; + private final DiscRepository discRepository; + private final SpotifyRepository spotifyRepository; + + @Autowired + SpotifyImportServiceImpl(final DiscRepository discRepository, + final SpotifyRepository spotifyRepository, + final GenreRepository genreRepository) { + this.genreRepository = genreRepository; + this.discRepository = discRepository; + this.spotifyRepository = spotifyRepository; + } + + + public void importDiscsByGenres(ImportErrorCallback importErrorCallback) { + if (checkAlreadyImported(importErrorCallback)) return; + try { + executeImportDiscs(importErrorCallback); + } catch (NoGenresException e) { + importErrorCallback.error(e); + } + } + + private boolean checkAlreadyImported(ImportErrorCallback importErrorCallback) { + // Poderia ser realizado uma consistencia mais adequada para verificar se os discos já foram importados. + if (discRepository.count() > 0) { + importErrorCallback.error(new AlreadyImportedDiscsException()); + return true; + } + return false; + } + + private void executeImportDiscs(ImportErrorCallback importErrorCallback) throws NoGenresException { + ExecutorService executorService = createExecutorToImportDiscs(); + genreRepository.findAll().forEach(genre -> executorService.submit(() -> { + try { + importGenre(genre); + } catch (IOException | SpotifyWebApiException e) { + importErrorCallback.error(new ImportedException(e)); + } + })); + executorService.shutdown(); + awaitImport(importErrorCallback, executorService); + } + + private ExecutorService createExecutorToImportDiscs() throws NoGenresException { + int value = Long.valueOf(genreRepository.count()).intValue(); + if (value > 0) { + return Executors.newFixedThreadPool(value); + } else { + throw new NoGenresException(); + } + } + + private void importGenre(GenreEntity genre) throws IOException, SpotifyWebApiException { + getFirstsItemsByGenre(genre).parallelStream().forEach(track -> saveDisc(track, genre)); + } + + private List getFirstsItemsByGenre(GenreEntity genre) throws IOException, SpotifyWebApiException { + return Arrays.asList(spotifyRepository.findTrackByGenre(genre.getName()).getItems()); + } + + private void saveDisc(Track track, GenreEntity genreEntity) { + DiscEntity discEntity = convertToDisc(track, genreEntity); + discRepository.save(discEntity); + } + + private DiscEntity convertToDisc(Track track, GenreEntity genreEntity) { + DiscEntity discEntity = trackToDiscEntity(track); + discEntity.setGenreEntity(genreEntity); + discEntity.setPrice(PriceUtil.generateRandomPriceDisc()); + return discEntity; + } + + private void awaitImport(ImportErrorCallback importErrorCallback, ExecutorService executorService) { + try { + executorService.awaitTermination(2, TimeUnit.MINUTES); + } catch (InterruptedException in) { + importErrorCallback.error(new ImportedException(in)); + } + } +} diff --git a/src/main/java/br/com/beblue/musicstore/settings/ApplicationStartup.java b/src/main/java/br/com/beblue/musicstore/settings/ApplicationStartup.java index 6be8046..f5a3156 100644 --- a/src/main/java/br/com/beblue/musicstore/settings/ApplicationStartup.java +++ b/src/main/java/br/com/beblue/musicstore/settings/ApplicationStartup.java @@ -2,6 +2,8 @@ import br.com.beblue.musicstore.exception.ImportedException; import br.com.beblue.musicstore.service.SpotifyImportService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.context.ApplicationListener; @@ -10,12 +12,14 @@ import java.util.concurrent.TimeUnit; @Component -public class ApplicationStartup implements ApplicationListener { +class ApplicationStartup implements ApplicationListener { + + private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationStartup.class.getName()); private final SpotifyImportService spotifyService; @Autowired - public ApplicationStartup(SpotifyImportService spotifyService) { + ApplicationStartup(SpotifyImportService spotifyService) { this.spotifyService = spotifyService; } @@ -23,7 +27,7 @@ public ApplicationStartup(SpotifyImportService spotifyService) { public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) { long time = System.currentTimeMillis(); spotifyService.importDiscsByGenres(ImportedException::printStackTrace); - System.out.println("Import time: " + TimeUnit.MILLISECONDS.toSeconds((System.currentTimeMillis() - time)) + "seg"); - System.out.println("----Application started----"); + LOGGER.info("Import time: " + TimeUnit.MILLISECONDS.toSeconds((System.currentTimeMillis() - time)) + "seg"); + LOGGER.info("----Application started----"); } } diff --git a/src/main/java/br/com/beblue/musicstore/settings/amq/SaleConfig.java b/src/main/java/br/com/beblue/musicstore/settings/amq/SaleConfig.java index 360e97f..6ef3e69 100644 --- a/src/main/java/br/com/beblue/musicstore/settings/amq/SaleConfig.java +++ b/src/main/java/br/com/beblue/musicstore/settings/amq/SaleConfig.java @@ -4,19 +4,18 @@ import org.springframework.amqp.core.BindingBuilder; import org.springframework.amqp.core.Queue; import org.springframework.amqp.core.TopicExchange; -import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.DependsOn; -import static br.com.beblue.musicstore.util.BeanConst.BEAN_SALE_QUEUE; -import static br.com.beblue.musicstore.util.BeanConst.BEAN_SALE_EXCHANGE; +import static br.com.beblue.musicstore.util.BeansConst.BEAN_SALE_EXCHANGE; +import static br.com.beblue.musicstore.util.BeansConst.BEAN_SALE_QUEUE; import static br.com.beblue.musicstore.util.ResourceConst.*; @Configuration -public class SaleConfig { +class SaleConfig { @Value(KEY_APPLICATION_MQ_SALE_QUEUE_NAME) private String queueName; diff --git a/src/main/java/br/com/beblue/musicstore/settings/exception/ProjectExceptionsHandler.java b/src/main/java/br/com/beblue/musicstore/settings/exception/ProjectExceptionsHandler.java index 6896c50..625367d 100644 --- a/src/main/java/br/com/beblue/musicstore/settings/exception/ProjectExceptionsHandler.java +++ b/src/main/java/br/com/beblue/musicstore/settings/exception/ProjectExceptionsHandler.java @@ -1,6 +1,6 @@ package br.com.beblue.musicstore.settings.exception; -import br.com.beblue.musicstore.dto.HttpResponseException; +import br.com.beblue.musicstore.controller.dto.HttpResponseException; import br.com.beblue.musicstore.exception.IllegalDateException; import br.com.beblue.musicstore.exception.NoValuePresentException; import org.springframework.http.HttpStatus; @@ -10,7 +10,7 @@ import org.springframework.web.bind.annotation.ResponseStatus; @ControllerAdvice -public class ProjectExceptionsHandler { +class ProjectExceptionsHandler { @ResponseStatus(HttpStatus.BAD_REQUEST) @ExceptionHandler(value = NoValuePresentException.class) diff --git a/src/main/java/br/com/beblue/musicstore/settings/swagger/SwaggerConfig.java b/src/main/java/br/com/beblue/musicstore/settings/swagger/SwaggerConfig.java index 626f998..83214bf 100644 --- a/src/main/java/br/com/beblue/musicstore/settings/swagger/SwaggerConfig.java +++ b/src/main/java/br/com/beblue/musicstore/settings/swagger/SwaggerConfig.java @@ -15,7 +15,7 @@ @Configuration @EnableSwagger2 -public class SwaggerConfig { +class SwaggerConfig { @Value("${application.version}") private String version; diff --git a/src/main/java/br/com/beblue/musicstore/spotify/SpotifyAPI.java b/src/main/java/br/com/beblue/musicstore/spotify/SpotifyAPI.java index a1f666e..6db46b8 100644 --- a/src/main/java/br/com/beblue/musicstore/spotify/SpotifyAPI.java +++ b/src/main/java/br/com/beblue/musicstore/spotify/SpotifyAPI.java @@ -21,8 +21,8 @@ public class SpotifyAPI { private final String clientSecret; private Credentials credentials; - public SpotifyAPI(@Value(KEY_SPOTIFY_CLIENT_ID) String clientId, - @Value(KEY_SPOTIFY_CLIENT_SECRET) String clientSecret + SpotifyAPI(@Value(KEY_SPOTIFY_CLIENT_ID) final String clientId, + @Value(KEY_SPOTIFY_CLIENT_SECRET) final String clientSecret ) { this.clientId = clientId; this.clientSecret = clientSecret; diff --git a/src/main/java/br/com/beblue/musicstore/util/BeanConst.java b/src/main/java/br/com/beblue/musicstore/util/BeansConst.java similarity index 74% rename from src/main/java/br/com/beblue/musicstore/util/BeanConst.java rename to src/main/java/br/com/beblue/musicstore/util/BeansConst.java index 5b2f326..e3c6866 100644 --- a/src/main/java/br/com/beblue/musicstore/util/BeanConst.java +++ b/src/main/java/br/com/beblue/musicstore/util/BeansConst.java @@ -1,6 +1,9 @@ package br.com.beblue.musicstore.util; -public class BeanConst { +public class BeansConst { + private BeansConst() { + } + public static final String BEAN_SALE_QUEUE = "saleQueue"; public static final String BEAN_SALE_EXCHANGE = "saleExchange"; } diff --git a/src/main/java/br/com/beblue/musicstore/util/DateUtil.java b/src/main/java/br/com/beblue/musicstore/util/DateUtil.java index d8eae57..74a454f 100644 --- a/src/main/java/br/com/beblue/musicstore/util/DateUtil.java +++ b/src/main/java/br/com/beblue/musicstore/util/DateUtil.java @@ -3,6 +3,9 @@ import java.util.Calendar; public class DateUtil { + private DateUtil() { + } + public static int getDayOfWeek() { return Calendar.getInstance().get(Calendar.DAY_OF_WEEK); } diff --git a/src/main/java/br/com/beblue/musicstore/util/PriceUtil.java b/src/main/java/br/com/beblue/musicstore/util/PriceUtil.java index 7ef6bbf..78c60f1 100644 --- a/src/main/java/br/com/beblue/musicstore/util/PriceUtil.java +++ b/src/main/java/br/com/beblue/musicstore/util/PriceUtil.java @@ -3,6 +3,9 @@ import java.util.Random; public class PriceUtil { + private PriceUtil() { + } + public static Double generateRandomPriceDisc() { int rangeMin = 10; int rangeMax = 100; diff --git a/src/main/java/br/com/beblue/musicstore/util/ResourceConst.java b/src/main/java/br/com/beblue/musicstore/util/ResourceConst.java index 4f26c81..1e559f4 100644 --- a/src/main/java/br/com/beblue/musicstore/util/ResourceConst.java +++ b/src/main/java/br/com/beblue/musicstore/util/ResourceConst.java @@ -1,6 +1,9 @@ package br.com.beblue.musicstore.util; public class ResourceConst { + private ResourceConst() { + } + public static final String ACTIVE_PROFILES_TEST_VALUE = "test"; public static final String KEY_SPOTIFY_CLIENT_ID = "${spotify.client.id}"; public static final String KEY_SPOTIFY_CLIENT_SECRET = "${spotify.client.secret}"; diff --git a/src/main/java/br/com/beblue/musicstore/util/converter/DiscSaleConverter.java b/src/main/java/br/com/beblue/musicstore/util/converter/DiscSaleConverter.java deleted file mode 100644 index b7f4bf4..0000000 --- a/src/main/java/br/com/beblue/musicstore/util/converter/DiscSaleConverter.java +++ /dev/null @@ -1,26 +0,0 @@ -package br.com.beblue.musicstore.util.converter; - -import br.com.beblue.musicstore.dto.DiscDTO; -import br.com.beblue.musicstore.model.entity.DiscEntity; -import br.com.beblue.musicstore.model.entity.DiscSaleEntity; -import com.wrapper.spotify.model_objects.specification.ArtistSimplified; -import com.wrapper.spotify.model_objects.specification.Track; -import org.springframework.data.domain.Page; - -import java.util.Arrays; -import java.util.List; -import java.util.stream.Collectors; - -import static br.com.beblue.musicstore.util.converter.GenreConverter.genreEntityToGenreDTO; - -public class DiscSaleConverter { - - public static List discsSalesEntitiesToDiscsDTO(List entities) { - return entities.stream().map(DiscSaleConverter::discEntityToDiscDTO).collect(Collectors.toList()); - } - - public static DiscDTO discEntityToDiscDTO(DiscSaleEntity discEntity) { - return new DiscDTO(discEntity.getDiscEntity().getId(), discEntity.getDiscEntity().getName(), discEntity.getDiscEntity().getArtist(), - genreEntityToGenreDTO(discEntity.getDiscEntity().getGenreEntity()), discEntity.getPrice(), discEntity.getCashback()); - } -} diff --git a/src/main/java/br/com/beblue/musicstore/util/converter/DiscConverter.java b/src/main/java/br/com/beblue/musicstore/util/mapper/DiscMapper.java similarity index 70% rename from src/main/java/br/com/beblue/musicstore/util/converter/DiscConverter.java rename to src/main/java/br/com/beblue/musicstore/util/mapper/DiscMapper.java index 34037ce..ab6407b 100644 --- a/src/main/java/br/com/beblue/musicstore/util/converter/DiscConverter.java +++ b/src/main/java/br/com/beblue/musicstore/util/mapper/DiscMapper.java @@ -1,18 +1,20 @@ -package br.com.beblue.musicstore.util.converter; +package br.com.beblue.musicstore.util.mapper; -import br.com.beblue.musicstore.dto.DiscDTO; +import br.com.beblue.musicstore.controller.dto.DiscDTO; import br.com.beblue.musicstore.model.entity.DiscEntity; import com.wrapper.spotify.model_objects.specification.ArtistSimplified; import com.wrapper.spotify.model_objects.specification.Track; import org.springframework.data.domain.Page; import java.util.Arrays; -import java.util.List; import java.util.stream.Collectors; -import static br.com.beblue.musicstore.util.converter.GenreConverter.*; +import static br.com.beblue.musicstore.util.mapper.GenreMapper.*; -public class DiscConverter { +public class DiscMapper { + + private DiscMapper() { + } public static DiscEntity trackToDiscEntity(Track track) { DiscEntity discEntity = new DiscEntity(); @@ -25,12 +27,8 @@ public static DiscEntity trackToDiscEntity(Track track) { return discEntity; } - public static List discEntitiesToDiscsDTO(List entities) { - return entities.stream().map(DiscConverter::discEntityToDiscDTO).collect(Collectors.toList()); - } - public static Page pageEntityToPageDTO(Page entities) { - return entities.map(DiscConverter::discEntityToDiscDTO); + return entities.map(DiscMapper::discEntityToDiscDTO); } public static DiscDTO discEntityToDiscDTO(DiscEntity discEntity) { diff --git a/src/main/java/br/com/beblue/musicstore/util/mapper/DiscSaleMapper.java b/src/main/java/br/com/beblue/musicstore/util/mapper/DiscSaleMapper.java new file mode 100644 index 0000000..a940b96 --- /dev/null +++ b/src/main/java/br/com/beblue/musicstore/util/mapper/DiscSaleMapper.java @@ -0,0 +1,23 @@ +package br.com.beblue.musicstore.util.mapper; + +import br.com.beblue.musicstore.controller.dto.DiscDTO; +import br.com.beblue.musicstore.model.entity.DiscSaleEntity; + +import java.util.List; +import java.util.stream.Collectors; + +import static br.com.beblue.musicstore.util.mapper.GenreMapper.genreEntityToGenreDTO; + +public class DiscSaleMapper { + private DiscSaleMapper() { + } + + public static List discsSalesEntitiesToDiscsDTO(List entities) { + return entities.stream().map(DiscSaleMapper::discEntityToDiscDTO).collect(Collectors.toList()); + } + + public static DiscDTO discEntityToDiscDTO(DiscSaleEntity discEntity) { + return new DiscDTO(discEntity.getDiscEntity().getId(), discEntity.getDiscEntity().getName(), discEntity.getDiscEntity().getArtist(), + genreEntityToGenreDTO(discEntity.getDiscEntity().getGenreEntity()), discEntity.getPrice(), discEntity.getCashback()); + } +} diff --git a/src/main/java/br/com/beblue/musicstore/util/converter/GenreConverter.java b/src/main/java/br/com/beblue/musicstore/util/mapper/GenreMapper.java similarity index 56% rename from src/main/java/br/com/beblue/musicstore/util/converter/GenreConverter.java rename to src/main/java/br/com/beblue/musicstore/util/mapper/GenreMapper.java index 2cfec67..660d547 100644 --- a/src/main/java/br/com/beblue/musicstore/util/converter/GenreConverter.java +++ b/src/main/java/br/com/beblue/musicstore/util/mapper/GenreMapper.java @@ -1,9 +1,12 @@ -package br.com.beblue.musicstore.util.converter; +package br.com.beblue.musicstore.util.mapper; -import br.com.beblue.musicstore.dto.GenreDTO; +import br.com.beblue.musicstore.controller.dto.GenreDTO; import br.com.beblue.musicstore.model.entity.GenreEntity; -public class GenreConverter { +public class GenreMapper { + private GenreMapper() { + } + public static GenreDTO genreEntityToGenreDTO(GenreEntity genreEntity) { return new GenreDTO(genreEntity.getId(), genreEntity.getName()); } diff --git a/src/main/java/br/com/beblue/musicstore/util/converter/SaleConverter.java b/src/main/java/br/com/beblue/musicstore/util/mapper/SaleMapper.java similarity index 61% rename from src/main/java/br/com/beblue/musicstore/util/converter/SaleConverter.java rename to src/main/java/br/com/beblue/musicstore/util/mapper/SaleMapper.java index 8a10407..abc788d 100644 --- a/src/main/java/br/com/beblue/musicstore/util/converter/SaleConverter.java +++ b/src/main/java/br/com/beblue/musicstore/util/mapper/SaleMapper.java @@ -1,20 +1,23 @@ -package br.com.beblue.musicstore.util.converter; +package br.com.beblue.musicstore.util.mapper; -import br.com.beblue.musicstore.dto.SaleResponseDTO; +import br.com.beblue.musicstore.controller.dto.SaleResponseDTO; import br.com.beblue.musicstore.model.entity.SaleEntity; import org.springframework.data.domain.Page; -public class SaleConverter { +public class SaleMapper { + private SaleMapper() { + } + public static SaleResponseDTO saleEntityToSaleResponseDTO(SaleEntity entity) { SaleResponseDTO response = new SaleResponseDTO(); response.setOrderNumber(entity.getUuid()); response.setCashbackPrice(entity.getCashback_price()); response.setTotalPrice(entity.getTotal_price()); - response.setProducts(DiscSaleConverter.discsSalesEntitiesToDiscsDTO(entity.getDiscSaleEntities())); + response.setProducts(DiscSaleMapper.discsSalesEntitiesToDiscsDTO(entity.getDiscSaleEntities())); return response; } public static Page saleListEntityToSaleResponseList(Page list) { - return list.map(SaleConverter::saleEntityToSaleResponseDTO); + return list.map(SaleMapper::saleEntityToSaleResponseDTO); } } diff --git a/src/test/java/br/com/beblue/musicstore/controller/HomeControllerTest.java b/src/test/java/br/com/beblue/musicstore/controller/HomeControllerTest.java deleted file mode 100644 index ae2994a..0000000 --- a/src/test/java/br/com/beblue/musicstore/controller/HomeControllerTest.java +++ /dev/null @@ -1,33 +0,0 @@ -package br.com.beblue.musicstore.controller; - -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; -import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.result.MockMvcResultHandlers; - -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; - -@WebMvcTest(HomeController.class) -class HomeControllerTest { - - @Autowired - MockMvc mockMvc; - - @Test - void test_home_message() throws Exception { - mockMvc.perform(get(HomeController.ROOT_PATH)) - .andDo(MockMvcResultHandlers.print()) - .andExpect(status().isOk()) - .andExpect(content().string(HomeController.HOME_MESSAGE)); - } - - @Test - void test_not_allowed_post_method() throws Exception { - mockMvc.perform(post(HomeController.ROOT_PATH)) - .andExpect(status().isMethodNotAllowed()); - } -} diff --git a/src/test/java/br/com/beblue/musicstore/controller/DiscControllerTest.java b/src/test/java/br/com/beblue/musicstore/controller/rest/DiscRestControllerTest.java similarity index 60% rename from src/test/java/br/com/beblue/musicstore/controller/DiscControllerTest.java rename to src/test/java/br/com/beblue/musicstore/controller/rest/DiscRestControllerTest.java index 6cf844c..feb31a9 100644 --- a/src/test/java/br/com/beblue/musicstore/controller/DiscControllerTest.java +++ b/src/test/java/br/com/beblue/musicstore/controller/rest/DiscRestControllerTest.java @@ -1,4 +1,4 @@ -package br.com.beblue.musicstore.controller; +package br.com.beblue.musicstore.controller.rest; import br.com.beblue.musicstore.exception.NoValuePresentException; import br.com.beblue.musicstore.service.DiscService; @@ -16,8 +16,8 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -@WebMvcTest(DiscController.class) -class DiscControllerTest { +@WebMvcTest(DiscRestController.class) +class DiscRestControllerTest { private static final String GENRE_ROCK = "rock"; @@ -28,38 +28,38 @@ class DiscControllerTest { DiscService discService; @Test - void test_not_allowed_post_method() throws Exception { - mockMvc.perform(post(DiscController.ROOT_PATH)) + void shouldNotAllowedPostMethod() throws Exception { + mockMvc.perform(post(DiscRestController.ROOT_PATH)) .andExpect(status().isMethodNotAllowed()); } @Test - void test_method_get_by_id() throws Exception { - mockMvc.perform(get(DiscController.ROOT_PATH + DiscController.PATH_BY_ID, 1)) + void shouldReturnStatusOkByGetDiscByID() throws Exception { + mockMvc.perform(get(DiscRestController.ROOT_PATH + DiscRestController.PATH_BY_ID, 1)) .andExpect(status().isOk()); } @Test - void test_method_get_by_id_with_no_value_exception() throws Exception { - int userId = 555; - NoValuePresentException exception = new NoValuePresentException("no value for id=" + userId); - when(discService.getDisc(userId)).thenThrow(exception); + void shouldReturnNoValueExceptionWhenCallMethodGetByIDWithNotFoundID() throws Exception { + int discID = 555; + NoValuePresentException exception = new NoValuePresentException("no value for id=" + discID); + when(discService.getDiscByID(discID)).thenThrow(exception); - mockMvc.perform(get(DiscController.ROOT_PATH + DiscController.PATH_BY_ID, userId)) + mockMvc.perform(get(DiscRestController.ROOT_PATH + DiscRestController.PATH_BY_ID, discID)) .andDo(print()) .andExpect(status().isBadRequest()) .andExpect(content().string(containsString(exception.getMessage()))); } @Test - void test_method_get_all() throws Exception { - mockMvc.perform(get(DiscController.ROOT_PATH)) + void shouldReturnOKWhenGetDiscsCalled() throws Exception { + mockMvc.perform(get(DiscRestController.ROOT_PATH)) .andExpect(status().isOk()); } @Test - void test_method_get_by_genre() throws Exception { - mockMvc.perform(get(DiscController.ROOT_PATH + DiscController.PATH_BY_GENRE, GENRE_ROCK)) + void shouldReturnOKWhenGetDiscsByGenresCalled() throws Exception { + mockMvc.perform(get(DiscRestController.ROOT_PATH + DiscRestController.PATH_BY_GENRE, GENRE_ROCK)) .andExpect(status().isOk()); } diff --git a/src/test/java/br/com/beblue/musicstore/controller/GenreControllerTest.java b/src/test/java/br/com/beblue/musicstore/controller/rest/GenreRestControllerTest.java similarity index 82% rename from src/test/java/br/com/beblue/musicstore/controller/GenreControllerTest.java rename to src/test/java/br/com/beblue/musicstore/controller/rest/GenreRestControllerTest.java index aa0b2d9..e80152b 100644 --- a/src/test/java/br/com/beblue/musicstore/controller/GenreControllerTest.java +++ b/src/test/java/br/com/beblue/musicstore/controller/rest/GenreRestControllerTest.java @@ -1,6 +1,6 @@ -package br.com.beblue.musicstore.controller; +package br.com.beblue.musicstore.controller.rest; -import br.com.beblue.musicstore.dto.GenreDTO; +import br.com.beblue.musicstore.controller.dto.GenreDTO; import br.com.beblue.musicstore.service.GenreService; import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.jupiter.api.Test; @@ -18,8 +18,8 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -@WebMvcTest(GenreController.class) -class GenreControllerTest { +@WebMvcTest(GenreRestController.class) +class GenreRestControllerTest { @Autowired MockMvc mockMvc; @@ -32,13 +32,13 @@ class GenreControllerTest { @Test void test_not_allowed_post_method() throws Exception { - mockMvc.perform(post(GenreController.ROOT_PATH)) + mockMvc.perform(post(GenreRestController.ROOT_PATH)) .andExpect(status().isMethodNotAllowed()); } @Test void test_allowed_get_method() throws Exception { - mockMvc.perform(get(GenreController.ROOT_PATH)) + mockMvc.perform(get(GenreRestController.ROOT_PATH)) .andExpect(status().isOk()); } @@ -46,7 +46,7 @@ void test_allowed_get_method() throws Exception { void test_list_genres() throws Exception { List genres = getFakeGenres(); Mockito.when(genreService.getAllGenres()).thenReturn(genres); - mockMvc.perform(get(GenreController.ROOT_PATH)) + mockMvc.perform(get(GenreRestController.ROOT_PATH)) .andExpect(status().isOk()) .andExpect(content().string(objectMapper.writeValueAsString(genres))); } diff --git a/src/test/java/br/com/beblue/musicstore/controller/SaleControllerTest.java b/src/test/java/br/com/beblue/musicstore/controller/rest/SaleRestControllerTest.java similarity index 84% rename from src/test/java/br/com/beblue/musicstore/controller/SaleControllerTest.java rename to src/test/java/br/com/beblue/musicstore/controller/rest/SaleRestControllerTest.java index 285f16c..f6fc635 100644 --- a/src/test/java/br/com/beblue/musicstore/controller/SaleControllerTest.java +++ b/src/test/java/br/com/beblue/musicstore/controller/rest/SaleRestControllerTest.java @@ -1,7 +1,7 @@ -package br.com.beblue.musicstore.controller; +package br.com.beblue.musicstore.controller.rest; -import br.com.beblue.musicstore.dto.SaleRequestDTO; -import br.com.beblue.musicstore.dto.SaleResponseDTO; +import br.com.beblue.musicstore.controller.dto.SaleRequestDTO; +import br.com.beblue.musicstore.controller.dto.SaleResponseDTO; import br.com.beblue.musicstore.service.SaleSearchService; import br.com.beblue.musicstore.service.SaleService; import com.fasterxml.jackson.databind.ObjectMapper; @@ -22,8 +22,8 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -@WebMvcTest(SaleController.class) -class SaleControllerTest { +@WebMvcTest(SaleRestController.class) +class SaleRestControllerTest { private static final String ORDER_NUMBER = "4124154-4124GW-GE32GG3-3G3G"; @@ -46,7 +46,7 @@ void test_post_sale_method() throws Exception { when(saleService.registerOrder(any())).thenReturn(saleResponseDTO); saleRequestDTO.setDiscsIds(Arrays.asList(1, 2)); - mockMvc.perform(post(SaleController.ROOT_PATH) + mockMvc.perform(post(SaleRestController.ROOT_PATH) .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(saleRequestDTO))) @@ -60,7 +60,7 @@ void test_post_sale_method() throws Exception { void test_get_sale_by_order_number_method() throws Exception { SaleResponseDTO saleResponseDTO = getSaleResponseTest(); when(saleSearchService.getOrderByNumber(ORDER_NUMBER)).thenReturn(saleResponseDTO); - mockMvc.perform(get(SaleController.ROOT_PATH + SaleController.PATH_BY_ORDER_NUMBER, ORDER_NUMBER)) + mockMvc.perform(get(SaleRestController.ROOT_PATH + SaleRestController.PATH_BY_ORDER_NUMBER, ORDER_NUMBER)) .andDo(print()) .andExpect(status().isOk()) .andExpect(content().string(objectMapper.writeValueAsString(saleResponseDTO))); @@ -70,7 +70,7 @@ void test_get_sale_by_order_number_method() throws Exception { void test_get_sales_by_date() throws Exception { String startDate = "2012-04-21"; String endDate = "2012-04-22"; - mockMvc.perform(get(SaleController.ROOT_PATH + SaleController.PATH_BY_DATE, startDate, endDate)) + mockMvc.perform(get(SaleRestController.ROOT_PATH + SaleRestController.PATH_BY_DATE, startDate, endDate)) .andExpect(status().isOk()); } diff --git a/src/test/java/br/com/beblue/musicstore/service/CashbackServiceTest.java b/src/test/java/br/com/beblue/musicstore/service/CashbackServiceTest.java deleted file mode 100644 index d0616b0..0000000 --- a/src/test/java/br/com/beblue/musicstore/service/CashbackServiceTest.java +++ /dev/null @@ -1,7 +0,0 @@ -package br.com.beblue.musicstore.service; - -import static org.junit.jupiter.api.Assertions.*; - -class CashbackServiceTest { - -} \ No newline at end of file diff --git a/src/test/java/br/com/beblue/musicstore/service/impl/CashbackServiceTest.java b/src/test/java/br/com/beblue/musicstore/service/impl/CashbackServiceTest.java new file mode 100644 index 0000000..575a29a --- /dev/null +++ b/src/test/java/br/com/beblue/musicstore/service/impl/CashbackServiceTest.java @@ -0,0 +1,5 @@ +package br.com.beblue.musicstore.service.impl; + +class CashbackServiceTest { + +} \ No newline at end of file diff --git a/src/test/java/br/com/beblue/musicstore/service/DiscServiceTest.java b/src/test/java/br/com/beblue/musicstore/service/impl/DiscServiceTest.java similarity index 85% rename from src/test/java/br/com/beblue/musicstore/service/DiscServiceTest.java rename to src/test/java/br/com/beblue/musicstore/service/impl/DiscServiceTest.java index 5b5072a..9cea4ef 100644 --- a/src/test/java/br/com/beblue/musicstore/service/DiscServiceTest.java +++ b/src/test/java/br/com/beblue/musicstore/service/impl/DiscServiceTest.java @@ -1,4 +1,4 @@ -package br.com.beblue.musicstore.service; +package br.com.beblue.musicstore.service.impl; import br.com.beblue.musicstore.settings.DiscsAppConfigurationsTests; import org.springframework.beans.factory.annotation.Autowired; @@ -12,7 +12,7 @@ class DiscServiceTest { @Autowired - DiscService discService; + DiscServiceImpl discService; } diff --git a/src/test/java/br/com/beblue/musicstore/service/SaleSearchServiceTest.java b/src/test/java/br/com/beblue/musicstore/service/impl/SaleSearchServiceTest.java similarity index 94% rename from src/test/java/br/com/beblue/musicstore/service/SaleSearchServiceTest.java rename to src/test/java/br/com/beblue/musicstore/service/impl/SaleSearchServiceTest.java index 6b3ac9d..92a485b 100644 --- a/src/test/java/br/com/beblue/musicstore/service/SaleSearchServiceTest.java +++ b/src/test/java/br/com/beblue/musicstore/service/impl/SaleSearchServiceTest.java @@ -1,8 +1,8 @@ -package br.com.beblue.musicstore.service; +package br.com.beblue.musicstore.service.impl; -import br.com.beblue.musicstore.dto.DiscDTO; -import br.com.beblue.musicstore.dto.GenreDTO; -import br.com.beblue.musicstore.dto.SaleResponseDTO; +import br.com.beblue.musicstore.controller.dto.DiscDTO; +import br.com.beblue.musicstore.controller.dto.GenreDTO; +import br.com.beblue.musicstore.controller.dto.SaleResponseDTO; import br.com.beblue.musicstore.exception.IllegalDateException; import br.com.beblue.musicstore.exception.NoValuePresentException; import br.com.beblue.musicstore.model.entity.DiscEntity; @@ -10,6 +10,7 @@ import br.com.beblue.musicstore.model.entity.GenreEntity; import br.com.beblue.musicstore.model.entity.SaleEntity; import br.com.beblue.musicstore.model.repository.SaleRepository; +import br.com.beblue.musicstore.service.impl.SaleSearchServiceImpl; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @@ -38,7 +39,7 @@ class SaleSearchServiceTest { private static final String UUID = "12456"; @Autowired - SaleSearchService saleSearchService; + SaleSearchServiceImpl saleSearchService; @MockBean SaleRepository saleRepository; diff --git a/src/test/java/br/com/beblue/musicstore/service/SaleServiceTest.java b/src/test/java/br/com/beblue/musicstore/service/impl/SaleServiceTest.java similarity index 92% rename from src/test/java/br/com/beblue/musicstore/service/SaleServiceTest.java rename to src/test/java/br/com/beblue/musicstore/service/impl/SaleServiceTest.java index b5c8b40..53e468b 100644 --- a/src/test/java/br/com/beblue/musicstore/service/SaleServiceTest.java +++ b/src/test/java/br/com/beblue/musicstore/service/impl/SaleServiceTest.java @@ -1,8 +1,8 @@ -package br.com.beblue.musicstore.service; +package br.com.beblue.musicstore.service.impl; -import br.com.beblue.musicstore.amq.SaleAMQSender; -import br.com.beblue.musicstore.dto.SaleRequestDTO; -import br.com.beblue.musicstore.dto.SaleResponseDTO; +import br.com.beblue.musicstore.events.SaleNotifiable; +import br.com.beblue.musicstore.controller.dto.SaleRequestDTO; +import br.com.beblue.musicstore.controller.dto.SaleResponseDTO; import br.com.beblue.musicstore.exception.NoValuePresentException; import br.com.beblue.musicstore.model.entity.DiscEntity; import br.com.beblue.musicstore.model.entity.GenreCashbackEntity; @@ -31,7 +31,7 @@ class SaleServiceTest { @Autowired - SaleService saleService; + SaleServiceImpl saleService; @MockBean DiscRepository discRepository; @@ -40,10 +40,10 @@ class SaleServiceTest { SaleRepository saleRepository; @MockBean - CashbackService cashbackService; + CashbackServiceImpl cashbackService; @MockBean - SaleAMQSender saleAMQSender; + SaleNotifiable saleNotifiable; @Test void test_no_value_present_exception_with_empty_list() { diff --git a/src/test/java/br/com/beblue/musicstore/service/SpotifyImportServiceTest.java b/src/test/java/br/com/beblue/musicstore/service/impl/SpotifyImportServiceTest.java similarity index 97% rename from src/test/java/br/com/beblue/musicstore/service/SpotifyImportServiceTest.java rename to src/test/java/br/com/beblue/musicstore/service/impl/SpotifyImportServiceTest.java index 9cd158f..1b7a06d 100644 --- a/src/test/java/br/com/beblue/musicstore/service/SpotifyImportServiceTest.java +++ b/src/test/java/br/com/beblue/musicstore/service/impl/SpotifyImportServiceTest.java @@ -1,4 +1,4 @@ -package br.com.beblue.musicstore.service; +package br.com.beblue.musicstore.service.impl; import br.com.beblue.musicstore.exception.AlreadyImportedDiscsException; import br.com.beblue.musicstore.exception.NoGenresException; @@ -37,7 +37,7 @@ class SpotifyImportServiceTest { SpotifyRepository spotifyRepository; @Autowired - SpotifyImportService service; + SpotifyImportServiceImpl service; @Test void test_import_discs_with_already_imported_data() { diff --git a/src/test/java/br/com/beblue/musicstore/util/converter/DiscConverterTest.java b/src/test/java/br/com/beblue/musicstore/util/mapper/DiscMapperTest.java similarity index 90% rename from src/test/java/br/com/beblue/musicstore/util/converter/DiscConverterTest.java rename to src/test/java/br/com/beblue/musicstore/util/mapper/DiscMapperTest.java index 5ced650..c1d5c58 100644 --- a/src/test/java/br/com/beblue/musicstore/util/converter/DiscConverterTest.java +++ b/src/test/java/br/com/beblue/musicstore/util/mapper/DiscMapperTest.java @@ -1,4 +1,4 @@ -package br.com.beblue.musicstore.util.converter; +package br.com.beblue.musicstore.util.mapper; import br.com.beblue.musicstore.model.entity.DiscEntity; import com.wrapper.spotify.model_objects.specification.AlbumSimplified; @@ -11,12 +11,12 @@ import static org.junit.jupiter.api.Assertions.*; -class DiscConverterTest { +class DiscMapperTest { @Test void trackToDiscEntity() { Track track = getCustomTrack(); - DiscEntity discEntity = DiscConverter.trackToDiscEntity(track); + DiscEntity discEntity = DiscMapper.trackToDiscEntity(track); assertEquals(discEntity.getName(), track.getAlbum().getName()); String artists = convertArtistsToString(track); assertEquals(discEntity.getArtist(), artists);