Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.46 KB | None | 0 0
  1. @Entity
  2. @Table(name = "wallet_info")
  3. public class WalletInfo {
  4.  
  5. @Id
  6. @Column(name = "id")
  7. @NotNull
  8. @GeneratedValue(strategy = GenerationType.AUTO)
  9. private Long id;
  10.  
  11. @NotNull
  12. @Column(name = "name")
  13. private String name;
  14.  
  15. @NotNull
  16. @Column(name = "address")
  17. private String address;
  18.  
  19. public Long getId() {
  20. return id;
  21. }
  22.  
  23. public void setId(Long id) {
  24. this.id = id;
  25. }
  26.  
  27. public String getName() {
  28. return name;
  29. }
  30.  
  31. public void setName(String name) {
  32. this.name = name;
  33. }
  34.  
  35. public String getAddress() {
  36. return address;
  37. }
  38.  
  39. public void setAddress(String address) {
  40. this.address = address;
  41. }
  42. }
  43.  
  44. public interface WalletInfoDao {
  45.  
  46. List<WalletInfo> getAllWallets();
  47.  
  48. WalletInfo getByName(String walletName);
  49.  
  50. WalletInfo create(String walletName, String address);
  51.  
  52. WalletInfo getById(Long id);
  53.  
  54. void deleteWalletInfoById(Long id);
  55. }
  56.  
  57. @Repository
  58. public class WalletInfoDaoHibernate
  59. extends HibernateDaoSupport
  60. implements WalletInfoDao {
  61.  
  62.  
  63. // .............
  64. // more methods
  65.  
  66.  
  67. @Override
  68. public List<WalletInfo> getAllWallets() {
  69. try (Session session = getSessionFactory().openSession()) {
  70. List<WalletInfo> result = session.createQuery("from WalletInfo").list();
  71. return result;
  72. }
  73. }
  74.  
  75.  
  76. @Override
  77. public WalletInfo create(String name, String address) {
  78.  
  79. WalletInfo walletInfo = new WalletInfo();
  80. walletInfo.setAddress(address);
  81. walletInfo.setName(name);
  82.  
  83. Transaction transaction = null;
  84.  
  85. try (Session session = getSessionFactory().openSession()) {
  86. transaction = session.beginTransaction();
  87. session.persist(walletInfo);
  88. transaction.commit();
  89. return walletInfo;
  90. } catch (RuntimeException e) {
  91. e.printStackTrace();
  92. if (transaction != null) {
  93. transaction.rollback();
  94. }
  95. throw e;
  96. }
  97. }
  98. }
  99.  
  100. curl -G http://localhost:8080/rest/wallets | json
  101.  
  102. @RequestMapping(value = "/wallets", method = RequestMethod.GET)
  103. public ResponseEntity<List<WalletInfoWrapper>> getAllWalletInfo() {
  104.  
  105. List<WalletInfo> walletInfos = walletService.getAllWallets();
  106.  
  107. if (Objects.isNull(walletInfos)) {
  108. return new ResponseEntity<List<WalletInfoWrapper>>(HttpStatus.NO_CONTENT);
  109. }
  110.  
  111. List<WalletInfoWrapper> walletInfoWrappers = new ArrayList<>();
  112.  
  113. // hiding the entity ids for the security purposes
  114. walletInfos.forEach(w -> walletInfoWrappers.add(new WalletInfoWrapper(w.getName(), w.getAddress())));
  115.  
  116. return new ResponseEntity<List<WalletInfoWrapper>>(walletInfoWrappers, HttpStatus.OK);
  117. }
  118.  
  119. [
  120. // ......
  121.  
  122. {
  123. "name": "{"name":"nuul"}",
  124. "address": "mwrcT2xHcRV5e2kLTLcYwQyWWwvQspgzsb"
  125. }
  126.  
  127. ]
  128.  
  129. $.ajax({
  130.  
  131. url: '/rest/wallets',
  132. method: 'GET',
  133. data: {
  134. name: name,
  135. address: addressName
  136. },
  137. success: function(result) {
  138. console.log(result)
  139. },
  140. error: function(err) {
  141. console.log(err)
  142. }
  143. })
  144.  
  145. @PostMapping(value = "/sendMoney/{walletId}")
  146. public ResponseEntity<WalletModelWrapper> sendMoneyByWalletId(@PathVariable("walletId") Long walletId,
  147. @RequestBody String amount, @RequestBody String address) {
  148. WalletModel walletModel = getWalletModel(walletId);
  149.  
  150. if (Objects.isNull(walletModel)) {
  151. return new ResponseEntity<WalletModelWrapper>(HttpStatus.NOT_FOUND);
  152. }
  153.  
  154. walletModel = walletService.sendMoney(walletId, amount, address);
  155.  
  156. WalletModelWrapper walletModelWrapper = new WalletModelWrapper();
  157. walletModelWrapper.setAddress(walletModel.getAddress().toString());
  158. walletModelWrapper.setBalance(walletModel.getBalance().toString());
  159.  
  160. return new ResponseEntity<WalletModelWrapper>(walletModelWrapper, HttpStatus.OK);
  161. }
  162.  
  163. $.ajax({
  164.  
  165. url: '/sendMoney/{walletId}',
  166. method: 'POST',
  167. data: {
  168. // dont know
  169. },
  170. success: function(result) {
  171. console.log(result)
  172. },
  173. error: function(err) {
  174. console.log(err)
  175. }
  176. })
  177.  
  178. @Configuration
  179. @EnableWebMvc
  180. @EnableTransactionManagement
  181. @ComponentScan(basePackages = {"mobi.puut.database"})
  182. public class DatabaseConfig {
  183.  
  184. // some more database config code
  185.  
  186. @Bean
  187. public LocalSessionFactoryBean sessionFactory() {
  188.  
  189. // mobi.puut.entities
  190. LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
  191. sessionFactory.setDataSource(dataSource());
  192. sessionFactory.setPackagesToScan(
  193. new String[]{"mobi.puut.entities"});
  194. sessionFactory.setHibernateProperties(hibernateProperties());
  195.  
  196. return sessionFactory;
  197. }
  198.  
  199.  
  200.  
  201. @Bean
  202. public DataSource dataSource() {
  203.  
  204. DriverManagerDataSource dataSource = new DriverManagerDataSource();
  205. dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
  206.  
  207. // dataSource.setUrl("jdbc:mysql://localhost:3306/wallet?createDatabaseIfNotExist=true");
  208. dataSource.setUrl("jdbc:mysql://localhost:3306/wallet");
  209. dataSource.setUsername("testuser");
  210. dataSource.setPassword("testpassword");
  211.  
  212. return dataSource;
  213. }
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement