Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Entity
- @Table(name = "wallet_info")
- public class WalletInfo {
- @Id
- @Column(name = "id")
- @NotNull
- @GeneratedValue(strategy = GenerationType.AUTO)
- private Long id;
- @NotNull
- @Column(name = "name")
- private String name;
- @NotNull
- @Column(name = "address")
- private String address;
- public Long getId() {
- return id;
- }
- public void setId(Long id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getAddress() {
- return address;
- }
- public void setAddress(String address) {
- this.address = address;
- }
- }
- public interface WalletInfoDao {
- List<WalletInfo> getAllWallets();
- WalletInfo getByName(String walletName);
- WalletInfo create(String walletName, String address);
- WalletInfo getById(Long id);
- void deleteWalletInfoById(Long id);
- }
- @Repository
- public class WalletInfoDaoHibernate
- extends HibernateDaoSupport
- implements WalletInfoDao {
- // .............
- // more methods
- @Override
- public List<WalletInfo> getAllWallets() {
- try (Session session = getSessionFactory().openSession()) {
- List<WalletInfo> result = session.createQuery("from WalletInfo").list();
- return result;
- }
- }
- @Override
- public WalletInfo create(String name, String address) {
- WalletInfo walletInfo = new WalletInfo();
- walletInfo.setAddress(address);
- walletInfo.setName(name);
- Transaction transaction = null;
- try (Session session = getSessionFactory().openSession()) {
- transaction = session.beginTransaction();
- session.persist(walletInfo);
- transaction.commit();
- return walletInfo;
- } catch (RuntimeException e) {
- e.printStackTrace();
- if (transaction != null) {
- transaction.rollback();
- }
- throw e;
- }
- }
- }
- curl -G http://localhost:8080/rest/wallets | json
- @RequestMapping(value = "/wallets", method = RequestMethod.GET)
- public ResponseEntity<List<WalletInfoWrapper>> getAllWalletInfo() {
- List<WalletInfo> walletInfos = walletService.getAllWallets();
- if (Objects.isNull(walletInfos)) {
- return new ResponseEntity<List<WalletInfoWrapper>>(HttpStatus.NO_CONTENT);
- }
- List<WalletInfoWrapper> walletInfoWrappers = new ArrayList<>();
- // hiding the entity ids for the security purposes
- walletInfos.forEach(w -> walletInfoWrappers.add(new WalletInfoWrapper(w.getName(), w.getAddress())));
- return new ResponseEntity<List<WalletInfoWrapper>>(walletInfoWrappers, HttpStatus.OK);
- }
- [
- // ......
- {
- "name": "{"name":"nuul"}",
- "address": "mwrcT2xHcRV5e2kLTLcYwQyWWwvQspgzsb"
- }
- ]
- $.ajax({
- url: '/rest/wallets',
- method: 'GET',
- data: {
- name: name,
- address: addressName
- },
- success: function(result) {
- console.log(result)
- },
- error: function(err) {
- console.log(err)
- }
- })
- @PostMapping(value = "/sendMoney/{walletId}")
- public ResponseEntity<WalletModelWrapper> sendMoneyByWalletId(@PathVariable("walletId") Long walletId,
- @RequestBody String amount, @RequestBody String address) {
- WalletModel walletModel = getWalletModel(walletId);
- if (Objects.isNull(walletModel)) {
- return new ResponseEntity<WalletModelWrapper>(HttpStatus.NOT_FOUND);
- }
- walletModel = walletService.sendMoney(walletId, amount, address);
- WalletModelWrapper walletModelWrapper = new WalletModelWrapper();
- walletModelWrapper.setAddress(walletModel.getAddress().toString());
- walletModelWrapper.setBalance(walletModel.getBalance().toString());
- return new ResponseEntity<WalletModelWrapper>(walletModelWrapper, HttpStatus.OK);
- }
- $.ajax({
- url: '/sendMoney/{walletId}',
- method: 'POST',
- data: {
- // dont know
- },
- success: function(result) {
- console.log(result)
- },
- error: function(err) {
- console.log(err)
- }
- })
- @Configuration
- @EnableWebMvc
- @EnableTransactionManagement
- @ComponentScan(basePackages = {"mobi.puut.database"})
- public class DatabaseConfig {
- // some more database config code
- @Bean
- public LocalSessionFactoryBean sessionFactory() {
- // mobi.puut.entities
- LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
- sessionFactory.setDataSource(dataSource());
- sessionFactory.setPackagesToScan(
- new String[]{"mobi.puut.entities"});
- sessionFactory.setHibernateProperties(hibernateProperties());
- return sessionFactory;
- }
- @Bean
- public DataSource dataSource() {
- DriverManagerDataSource dataSource = new DriverManagerDataSource();
- dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
- // dataSource.setUrl("jdbc:mysql://localhost:3306/wallet?createDatabaseIfNotExist=true");
- dataSource.setUrl("jdbc:mysql://localhost:3306/wallet");
- dataSource.setUsername("testuser");
- dataSource.setPassword("testpassword");
- return dataSource;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement