Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.Data;
  3. using WebApiDemo.Helpers;
  4. using WebApiDemo.Models;
  5. using WebApiDemo.Repositories;
  6.  
  7. namespace WebApiDemo.Services
  8. {
  9. public class TransactionService : ITransactionService
  10. {
  11. private ITransactionRepository _transactionRepository;
  12.  
  13. public TransactionService(ITransactionRepository transactionRepository)
  14. {
  15. _transactionRepository = transactionRepository;
  16. }
  17.  
  18. public List<Transaction> GetTransactionsByYear(int year)
  19. {
  20. var list = new List<Transaction>();
  21. var datatable = _transactionRepository.GetTransactionsByYear(year);
  22.  
  23. if (null == datatable || datatable.Rows.Count == 0)
  24. return list;
  25.  
  26. foreach (DataRow row in datatable.Rows)
  27. {
  28. list.Add(row.ToTransaction());
  29. }
  30. return list;
  31. }
  32.  
  33. public Transaction GetTransactionById(int transactionId)
  34. {
  35. var datatable = _transactionRepository.GetTransactionById(transactionId);
  36.  
  37. if (null == datatable || datatable.Rows.Count == 0)
  38. return null;
  39.  
  40. return datatable.Rows[0].ToTransaction();
  41. }
  42. }
  43.  
  44. public static class DataRowHelpers
  45. {
  46. public static Transaction ToTransaction(this DataRow row)
  47. {
  48. if (row != null)
  49. {
  50. return new Transaction
  51. {
  52. TransactionId = Convert.ToInt32(row["id"]),
  53. TransactionAmount = Convert.ToDecimal(row["amount"]),
  54. TransactionDate = Convert.ToDateTime(row["date"])
  55. };
  56. }
  57. return null;
  58. }
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement