Skip to content
This repository has been archived by the owner on Jan 17, 2022. It is now read-only.

Galina Isyanova #53

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Реализован перевод между аккаунтами через Rest-запрос.
GalinaIs committed Feb 28, 2021
commit b71aab03935d419d7682d391added8ca21af7e3d
20 changes: 20 additions & 0 deletions src/main/java/com/devexperts/config/PopulationAccountService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.devexperts.config;

import com.devexperts.account.Account;
import com.devexperts.account.AccountKey;
import com.devexperts.service.AccountService;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class PopulationAccountService {
@Bean
CommandLineRunner initClientDatabase(AccountService accountService) {
return args -> {
accountService.createAccount(new Account(AccountKey.valueOf(1L), "Ivan", "Ivanov", 500.0));
accountService.createAccount(new Account(AccountKey.valueOf(2L), "Petr", "Petrov", 350.0));
accountService.createAccount(new Account(AccountKey.valueOf(3L), "Igor", "Sidorov", 750.0));
};
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.devexperts.rest;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;

public abstract class AbstractAccountController {
abstract ResponseEntity<Void> transfer(long sourceId, long targetId, double amount);
abstract ResponseEntity<Void> transfer(@PathVariable Long sourceId, @PathVariable Long targetId, @PathVariable Double amount);
}
28 changes: 26 additions & 2 deletions src/main/java/com/devexperts/rest/AccountController.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,38 @@
package com.devexperts.rest;

import com.devexperts.account.Account;
import com.devexperts.service.AccountService;
import com.devexperts.service.exception.NotFoundAccountException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class AccountController extends AbstractAccountController {
private final AccountService accountService;

public ResponseEntity<Void> transfer(long sourceId, long targetId, double amount) {
return null;
public AccountController(AccountService accountService) {
this.accountService = accountService;
}

@PostMapping("/operations/transfer")
public ResponseEntity<Void> transfer(@RequestParam Long sourceId, @RequestParam Long targetId, @RequestParam Double amount) {
if (sourceId == null || targetId == null || amount == null || amount <= 0) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
try {
Account source = accountService.getAccount(sourceId);
Account target = accountService.getAccount(targetId);
accountService.transfer(source, target, amount);
return new ResponseEntity<>(HttpStatus.OK);
} catch (NotFoundAccountException e) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
} catch (Exception e) {
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}