Skip to content

Commit

Permalink
Merge pull request #9 from mariazevedo88/fix/bestPractices
Browse files Browse the repository at this point in the history
Fixing all Autowired fields.
  • Loading branch information
Mariana Azevedo authored Jul 3, 2020
2 parents af7e063 + 90dc7be commit 6088e19
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,18 @@
@RequestMapping("/financial/v1/statistics")
public class StatisticController {

@Autowired
private StatisticService service;
private StatisticService statisticService;

@Autowired
private TransactionService transactionService;

@Autowired
public StatisticController(StatisticService statisticService,
TransactionService transactionService) {

this.statisticService = statisticService;
this.transactionService = transactionService;
}

/**
* Method that creates statistics based on the transactions.
*
Expand Down Expand Up @@ -69,7 +75,7 @@ public ResponseEntity<Response<StatisticDTO>> create(@RequestHeader(value=Financ
Response<StatisticDTO> response = new Response<>();

Statistic statistics = createStatistics(transactionService.findAll());
statistics = service.save(statistics);
statistics = statisticService.save(statistics);

StatisticDTO dto = convertEntityToDTO(statistics);
createSelfLink(statistics, dto);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,12 @@ public class TransactionController {

private static final Logger logger = LoggerFactory.getLogger(TransactionController.class);

private TransactionService transactionService;

@Autowired
private TransactionService service;
public TransactionController(TransactionService transactionService) {
this.transactionService = transactionService;
}

/**
* Method that creates a transaction in the database.
Expand Down Expand Up @@ -100,7 +104,7 @@ public ResponseEntity<Response<TransactionDTO>> create(@RequestHeader(value=Fina
throw new NotParsableContentException("Date of the transaction is in the future.");
}

Transaction transaction = service.save(convertDTOToEntity(dto));
Transaction transaction = transactionService.save(convertDTOToEntity(dto));
TransactionDTO dtoSaved = convertEntityToDTO(transaction);
createSelfLink(transaction, dtoSaved);

Expand Down Expand Up @@ -159,14 +163,14 @@ public ResponseEntity<Response<TransactionDTO>> update(@RequestHeader(value=Fina
throw new NotParsableContentException("Date of the transaction is in the future.");
}

Optional<Transaction> transactionToFind = service.findById(dto.getId());
Optional<Transaction> transactionToFind = transactionService.findById(dto.getId());
if (!transactionToFind.isPresent()) {
throw new TransactionNotFoundException("Transaction id=" + dto.getId() + " not found");
} else if (transactionToFind.get().getId().compareTo(dto.getId()) != 0) {
throw new TransactionInvalidUpdateException("You don't have permission to change the transaction id=" + dto.getId());
}

Transaction transaction = service.save(convertDTOToEntity(dto));
Transaction transaction = transactionService.save(convertDTOToEntity(dto));
TransactionDTO itemDTO = convertEntityToDTO(transaction);

createSelfLink(transaction, itemDTO);
Expand Down Expand Up @@ -203,7 +207,7 @@ public ResponseEntity<Response<List<TransactionDTO>>> findAll(@RequestHeader(val

Response<List<TransactionDTO>> response = new Response<>();

List<Transaction> transactions = service.findAll();
List<Transaction> transactions = transactionService.findAll();

if (transactions.isEmpty()) {
throw new TransactionNotFoundException("There are no transactions registered in the database.");
Expand Down Expand Up @@ -257,7 +261,7 @@ public ResponseEntity<Response<List<TransactionDTO>>> findByNsu(@RequestHeader(v
throws TransactionNotFoundException {

Response<List<TransactionDTO>> response = new Response<>();
List<Transaction> transactions = service.findByNsu(transactionNSU);
List<Transaction> transactions = transactionService.findByNsu(transactionNSU);

if (transactions.isEmpty()) {
throw new TransactionNotFoundException("There are no transactions registered with the nsu=" + transactionNSU);
Expand Down Expand Up @@ -308,7 +312,7 @@ public ResponseEntity<Response<TransactionDTO>> findById(@RequestHeader(value=Fi
throws TransactionNotFoundException {

Response<TransactionDTO> response = new Response<>();
Optional<Transaction> transaction = service.findById(transactionId);
Optional<Transaction> transaction = transactionService.findById(transactionId);

if (!transaction.isPresent()) {
throw new TransactionNotFoundException("Transaction id=" + transactionId + " not found");
Expand Down Expand Up @@ -352,13 +356,13 @@ public ResponseEntity<Response<String>> delete(@RequestHeader(value=FinancialApi
throws TransactionNotFoundException {

Response<String> response = new Response<>();
Optional<Transaction> transaction = service.findById(transactionId);
Optional<Transaction> transaction = transactionService.findById(transactionId);

if (!transaction.isPresent()) {
throw new TransactionNotFoundException("Transaction id=" + transactionId + " not found");
}

service.deleteById(transactionId);
transactionService.deleteById(transactionId);
response.setData("Transaction id=" + transactionId + " successfully deleted");

MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@
* @author Mariana Azevedo
* @since 28/03/2020
*/
@Service
@Service("statisticService")
public class StatisticServiceImpl implements StatisticService {

@Autowired
private StatisticRepository statisticRepository;

@Autowired
public StatisticServiceImpl(StatisticRepository statisticRepository) {
this.statisticRepository = statisticRepository;
}

/**
* @see StatisticService#save(Statistic)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@
@Service
public class TransactionServiceImpl implements TransactionService {

private TransactionRepository transactionRepository;

@Autowired
private TransactionRepository repository;
public TransactionServiceImpl(TransactionRepository transactionRepository) {
this.transactionRepository = transactionRepository;
}

/**
* @see TransactionService#save(Transaction)
*/
@Override
public Transaction save(Transaction transaction) {
return repository.save(transaction);
return transactionRepository.save(transaction);
}

/**
Expand All @@ -37,15 +41,15 @@ public Transaction save(Transaction transaction) {
@Override
@Cacheable(value="transactionNsuCache", key="#nsu", unless="#result==null")
public List<Transaction> findByNsu(String nsu) {
return repository.findByNsu(nsu);
return transactionRepository.findByNsu(nsu);
}

/**
* @see TransactionService#deleteById(Long)
*/
@Override
public void deleteById(Long transactionId) {
repository.deleteById(transactionId);
transactionRepository.deleteById(transactionId);
}

/**
Expand All @@ -54,15 +58,15 @@ public void deleteById(Long transactionId) {
@Override
@Cacheable(value="transactionIdCache", key="#id", unless="#result==null")
public Optional<Transaction> findById(Long id) {
return repository.findById(id);
return transactionRepository.findById(id);
}

/**
* @see TransactionService#findAll()
*/
@Override
public List<Transaction> findAll() {
return repository.findAll();
return transactionRepository.findAll();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class FinancialApiConfiguration {
*
* @return <code>LinkDiscoverers</code> object
*/
@Bean
@Bean(name = "linkDiscover")
public LinkDiscoverers discoverers() {
List<LinkDiscoverer> plugins = new ArrayList<>();
plugins.add(new CollectionJsonLinkDiscoverer());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@
@Configuration
public class RateLimitingConfiguration implements WebMvcConfigurer {

@Autowired
private RateLimitInterceptor interceptor;

@Autowired
public RateLimitingConfiguration(RateLimitInterceptor interceptor) {
this.interceptor = interceptor;
}

/**
* Method that allow intercepting routes for rate limiting
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@
@Component
public class RateLimitInterceptor implements HandlerInterceptor {

@Autowired
private APIUsagePlansService pricingPlanService;

@Autowired
public RateLimitInterceptor(APIUsagePlansService pricingPlanService) {
this.pricingPlanService = pricingPlanService;
}

/**
* @see HandlerInterceptor#preHandle(HttpServletRequest, HttpServletResponse, Object)
Expand Down

0 comments on commit 6088e19

Please sign in to comment.