Advertisement
Guest User

Untitled

a guest
May 27th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. using IOweYou.Backend.Interfaces;
  2. using IOweYou.Backend.Models;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Web;
  8.  
  9. namespace IOweYou.Backend.Classes
  10. {
  11. public class DebtDataProvider : MySqlDataProvider, IDebtDataProvider
  12. {
  13. public IEnumerable<Friend> GetUserFriendsByUserId(User user)
  14. {
  15. List<Friend> Friends = null;
  16.  
  17. try
  18. {
  19. IDbCommand command = _dbConnection.CreateCommand();
  20. command.CommandText = @"SELECT
  21. d.`debt_id` AS `debt_id`,
  22. u.`user_id` AS `user_id`,
  23. u.`username` AS `user_username`,
  24. u.`full_name` AS `user_full_name`,
  25. f.`user_id` AS `friend_id`,
  26. f.`username` AS `friend_username`,
  27. f.`full_name` AS `friend_full_name`,
  28. d.`amount` AS `amount`,
  29. d.`registration_date` AS `registration_date`,
  30. d.`expiration_date` AS `expiration_date`
  31. FROM `debt` AS d
  32. LEFT OUTER JOIN `users` AS u ON d.`user_id` = u.`user_id`
  33. LEFT OUTER JOIN `users` AS f ON d.`friend_id` = f.`user_id`
  34. WHERE d.`user_id` = @UserId OR d.`friend_id` = @UserId; ";
  35.  
  36. IDbDataParameter userIdparam = command.CreateParameter();
  37. userIdparam.ParameterName = "@UserId";
  38. userIdparam.Value = user.UserId;
  39. IDataReader reader = command.ExecuteReader();
  40.  
  41. Friends = new List<Friend>();
  42.  
  43. while (reader.Read())
  44. {
  45. bool owedDebt = (int)reader["friend_id"] == user.UserId;
  46.  
  47. int friendId = owedDebt ? (int)reader["user_id"] : (int)reader["friend_id"];
  48.  
  49. Friend friend = Friends.Where(f => f.UserId == user.UserId).FirstOrDefault();
  50.  
  51. if (friend == null)
  52. {
  53. friend = new Friend()
  54. {
  55. UserId = (int)reader["user_id"],
  56. Username = (string)reader["username"],
  57. FullName = (string)reader["full_name"],
  58. Debts = new List<Debt>(),
  59. OwedDebts = new List<Debt>()
  60. };
  61. Friends.Add(friend);
  62. }
  63.  
  64. Debt debt = new Debt()
  65. {
  66. DebtId = (int)reader["debt_id"],
  67. RegistrationDate = (DateTime)reader["registration_date"],
  68. ExpirationDate = (DateTime)reader["expiration_date"],
  69. Amount = (decimal)reader["amount"]
  70. };
  71.  
  72. if (owedDebt)
  73. {
  74. friend.OwedDebts.Add(debt);
  75. }
  76. else
  77. {
  78. friend.Debts.Add(debt);
  79. }
  80. }
  81.  
  82. }
  83. catch (Exception ex)
  84. {
  85. Console.WriteLine(ex.Message);
  86. }
  87. return Friends;
  88. }
  89.  
  90. public bool UpdateUserFriends(User user, IEnumerable<Friend> friends)
  91. {
  92. throw new NotImplementedException();
  93. }
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement