Мини задача: сделать класс иммутабельным
class Person{
String name;
HashMapaddresses;
}
Провести кодревью:
package com.example.demo.model;
import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.Id;
Account.
@author Author
@Data
@Entity
public class Account {
@Id
private Long id;
private String accountNumber;
private String bic;
private Double amount;
}
package com.example.demo.repository;
import com.example.demo.model.Account;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
/
* AccountRepository.
* @author Author
*/
public interface AccountRepository extends JpaRepository {
List findByAccountNumber(String accountNumber);
}
package com.example.demo.service;
import com.example.demo.model.Account;
import com.example.demo.repository.AccountRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/
* BalanceService.
* @author Author
*/
@Service
public class BalanceService {
@Autowired
AccountRepository accountRepository;
public Account change(Long id, Double diff) {
Account account = accountRepository.getOne(id);
account.setAmount(account.getAmount() + diff);
return accountRepository.save(account);
}
package com.example.demo.resource;
import com.example.demo.model.Account;
import com.example.demo.repository.AccountRepository;
import com.example.demo.service.BalanceService;
import lombok.RequiredArgsConstructor;
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.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* AccountResource.
* @author Author
*/
@RestController
@RequiredArgsConstructor
public class AccountResource {
private final AccountRepository accountRepository;
private final BalanceService balanceService;
@GetMapping(value = "/accounts/add",
produces = MediaType.APPLICATIONJSONVALUE)
Account add(Account account) {
return accountRepository.save(account);
}
@GetMapping(value = "/accounts/{accountNumber}",
produces = MediaType.APPLICATIONJSONVALUE)
List findByAccountNumber(@PathVariable("accountNumber")
String accountNumber) {
return accountRepository.findByAccountNumber(accountNumber);
}
@PutMapping(value = "/accounts/{id}/change",
produces = MediaType.APPLICATIONJSONVALUE)
Account changeBalance(@PathVariable("id") Long id,
@RequestParam("diff") Double diff) {
return balanceService.change(id, diff);
}
}
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd">
<changeSet id="account" author="Author">
<preConditions onFail="MARK_RAN">
<not>
<tableExists tableName="account"/>
</not>
</preConditions>
<createTable tableName="account" remarks="Счета">
<column name="id" type="bigint"
remarks="Идентификатор записи">
<constraints primaryKey="true"
primaryKeyName="account_pk"
nullable="false"/>
</column>
<column name="account_number" type="varchar(50)"/>
<column name="bic" type="varchar(9)"/>
<column name="amount" type="numeric(17,2)"/>
</createTable>
</changeSet>
</databaseChangeLog>
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">