Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using IOweYou.Backend.Interfaces;
- using IOweYou.Backend.Models;
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- using System.Web;
- namespace IOweYou.Backend.Classes
- {
- public class DebtDataProvider : MySqlDataProvider, IDebtDataProvider
- {
- public IEnumerable<Friend> GetUserFriendsByUserId(User user)
- {
- List<Friend> Friends = null;
- try
- {
- IDbCommand command = _dbConnection.CreateCommand();
- command.CommandText = @"SELECT
- d.`debt_id` AS `debt_id`,
- u.`user_id` AS `user_id`,
- u.`username` AS `user_username`,
- u.`full_name` AS `user_full_name`,
- f.`user_id` AS `friend_id`,
- f.`username` AS `friend_username`,
- f.`full_name` AS `friend_full_name`,
- d.`amount` AS `amount`,
- d.`registration_date` AS `registration_date`,
- d.`expiration_date` AS `expiration_date`
- FROM `debt` AS d
- LEFT OUTER JOIN `users` AS u ON d.`user_id` = u.`user_id`
- LEFT OUTER JOIN `users` AS f ON d.`friend_id` = f.`user_id`
- WHERE d.`user_id` = @UserId OR d.`friend_id` = @UserId; ";
- IDbDataParameter userIdparam = command.CreateParameter();
- userIdparam.ParameterName = "@UserId";
- userIdparam.Value = user.UserId;
- IDataReader reader = command.ExecuteReader();
- Friends = new List<Friend>();
- while (reader.Read())
- {
- bool owedDebt = (int)reader["friend_id"] == user.UserId;
- int friendId = owedDebt ? (int)reader["user_id"] : (int)reader["friend_id"];
- Friend friend = Friends.Where(f => f.UserId == user.UserId).FirstOrDefault();
- if (friend == null)
- {
- friend = new Friend()
- {
- UserId = (int)reader["user_id"],
- Username = (string)reader["username"],
- FullName = (string)reader["full_name"],
- Debts = new List<Debt>(),
- OwedDebts = new List<Debt>()
- };
- Friends.Add(friend);
- }
- Debt debt = new Debt()
- {
- DebtId = (int)reader["debt_id"],
- RegistrationDate = (DateTime)reader["registration_date"],
- ExpirationDate = (DateTime)reader["expiration_date"],
- Amount = (decimal)reader["amount"]
- };
- if (owedDebt)
- {
- friend.OwedDebts.Add(debt);
- }
- else
- {
- friend.Debts.Add(debt);
- }
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- return Friends;
- }
- public bool UpdateUserFriends(User user, IEnumerable<Friend> friends)
- {
- throw new NotImplementedException();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement