public async Task <Friend> AddFriendAsync(int userid, int friendid) { Friend friend = new Friend(); var frdexist = _Context.friends.SingleOrDefault( c => (c.userId == userid && c.friendId == friendid) || (c.userId == friendid && c.friendId == userid)); if (frdexist == null) { friend.friendId = friendid; friend.userId = userid; _Context.friends.Add(friend); try { await _Context.SaveChangesAsync(); } catch (Exception exp) { _Logger.LogError($"Error in {nameof(AddFriendAsync)}: " + exp.Message); } } return(friend); }
public async Task <Friend> AddNewFriendAsync(string name, string email, int userid) { Friend friend = new Friend(); var user = await _Context.users.SingleOrDefaultAsync( c => c.email_id == email && c.user_name == name); if (user != null) { var frdexist = _Context.friends.SingleOrDefault( c => (c.userId == userid && c.friendId == user.userid) || (c.userId == user.userid && c.friendId == userid)); if (frdexist == null) { friend.friendId = user.userid; friend.userId = userid; _Context.friends.Add(friend); try { await _Context.SaveChangesAsync(); } catch (Exception exp) { _Logger.LogError($"Error in {nameof(AddNewFriendAsync)}: " + exp.Message); } } } return(friend); }
//public async Task<List<UserResponse>> GetUsersAsync() //{ // return await _Context.users // .Select(x => new UserResponse // { // userid = x.userid, // password = x.password, // email_id = x.email_id, // phone_no=x.phone_no, // user_name=x.user_name // } // ).ToListAsync(); //} //public async Task<User> InsertUserAsync(User user) //{ // _Context.users.Add(user); // try // { // await _Context.SaveChangesAsync(); // } // catch (Exception exp) // { // _Logger.LogError($"Error in {nameof(InsertUserAsync)}: " + exp.Message); // } // return user; //} public async Task <User> RegisterAsync(string name, string email, string password) { User user = new User(); var userexist = _Context.users.Where(c => c.email_id == email).SingleOrDefault(); if (userexist == null) { user.user_name = name; user.password = password; user.email_id = email; _Context.users.Add(user); try { await _Context.SaveChangesAsync(); } catch (Exception exp) { _Logger.LogError($"Error in {nameof(RegisterAsync)}: " + exp.Message); } } else { _Logger.LogError($"Error in {nameof(RegisterAsync)}"); } return(user); }
public async Task <GroupMember> AddGroupMemberAsync(int user, int group) { GroupMember groupMember = new GroupMember(); groupMember.groupId = group; groupMember.userId = user; var member = _Context.groupMembers.SingleOrDefault(c => c.userId == user && c.groupId == group); if (member == null) { _Context.groupMembers.Add(groupMember); } try { await _Context.SaveChangesAsync(); } catch (Exception exp) { _Logger.LogError($"Error in {nameof(AddGroupMemberAsync)}: " + exp.Message); } //for (int i = 0; i < group1.groupMembers.Count; i++) //{ // for (int j = i + 1; j < group1.groupMembers.Count; j++) // { var grpmem = _Context.groupMembers.Where(c => c.groupId == group).ToList(); for (int i = 0; i < grpmem.Count; i++) { Friend friend = new Friend(); var frdexist = _Context.friends.SingleOrDefault( c => (c.userId == user && c.friendId == grpmem[i].userId) || (c.userId == grpmem[i].userId && c.friendId == user)); if (frdexist == null) { friend.friendId = grpmem[i].userId; friend.userId = user; _Context.friends.Add(friend); try { await _Context.SaveChangesAsync(); } catch (Exception exp) { _Logger.LogError($"Error in {nameof(AddGroupMemberAsync)}: " + exp.Message); } } } return(groupMember); }
public async Task AddAsync(SelectRecord entity) { _context.Add(entity); await _context.SaveChangesAsync(); _context.DetachAll(); }
public async Task <GroupMember> AddGroupMemberAsync(int user, int group) { GroupMember groupMember = new GroupMember(); groupMember.groupId = group; groupMember.userId = user; var member = _Context.groupMembers.SingleOrDefault(c => c.userId == user && c.groupId == group); if (member == null) { _Context.groupMembers.Add(groupMember); } try { await _Context.SaveChangesAsync(); } catch (Exception exp) { _Logger.LogError($"Error in {nameof(AddGroupMemberAsync)}: " + exp.Message); } return(groupMember); }
public async Task <GroupResponse> CreateGroupAsync(CreateGroupResponse group1) { GroupResponse response = new GroupResponse(); Group group = new Group(); group.group_name = group1.group_name; group.group_created_by = group1.group_created_by; group.is_simplified_depts = group1.is_simplified_depts; _Context.groups.Add(group); try { await _Context.SaveChangesAsync(); } catch (Exception exp) { _Logger.LogError($"Error in {nameof(CreateGroupAsync)}: " + exp.Message); } foreach (var x in group1.groupMembers) { GroupMember groupMember = new GroupMember(); groupMember.groupId = group.groupid; groupMember.userId = x.userId; _Context.groupMembers.Add(groupMember); try { await _Context.SaveChangesAsync(); } catch (Exception exp) { _Logger.LogError($"Error in {nameof(CreateGroupAsync)}: " + exp.Message); } } response = ModelData(group); return(response); }
public async Task <bool> SettleUpAsync(TransactionResponse transaction) { Transaction transactions = new Transaction(); if (transaction.groupId != 0) { transactions.groupId = transaction.groupId; } transactions.paid_amount = transaction.paid_amount; transactions.payeeId = transaction.payeeId; transactions.payerId = transaction.payerId; transactions.paid_on = transaction.paid_on; _Context.transactions.Add(transactions); //await _Context.SaveChangesAsync(); var settle = await _Context.settlements.FirstOrDefaultAsync(c => c.payeeId == transactions.payeeId && c.payerId == transactions.payerId && c.groupId == transactions.groupId); if (settle != null) { settle.amount = settle.amount - transactions.paid_amount; _Context.settlements.Attach(settle); } else { if (transaction.groupId != 0) { Settlements settlements = new Settlements(); settlements.payerId = transactions.payeeId; settlements.payeeId = transactions.payerId; settlements.amount = transactions.paid_amount; settlements.groupId = transactions.groupId; var settle1 = await _Context.settlements.FirstOrDefaultAsync(c => c.payeeId == settlements.payeeId && c.payerId == settlements.payerId); if (settle1 != null) { settle1.amount = settle1.amount + transactions.paid_amount; _Context.settlements.Attach(settle1); } else { _Context.settlements.Add(settlements); } } else { Settlements settlements = new Settlements(); settlements.payerId = transactions.payeeId; settlements.payeeId = transactions.payerId; settlements.amount = transactions.paid_amount; var settle1 = await _Context.settlements.FirstOrDefaultAsync(c => c.payeeId == settlements.payeeId && c.payerId == settlements.payerId); if (settle1 != null) { settle1.amount = settle1.amount + transactions.paid_amount; _Context.settlements.Attach(settle1); } else { _Context.settlements.Add(settlements); } } } try { return(await _Context.SaveChangesAsync() > 0 ? true : false); } catch (Exception exp) { _Logger.LogError($"Error in {nameof(SettleUpAsync)}: " + exp.Message); } return(false); }
public async Task <int> SaveAsync() { return(await _dbContext.SaveChangesAsync()); }
public async Task <Bill> AddNewBillAsync(BillResponse billResponse) { Bill bill = new Bill(); if (billResponse.groupId != 0) { bill.groupId = billResponse.groupId; } bill.bill_created_at = billResponse.bill_created_at; bill.bill_created_byId = billResponse.bill_created_byId; bill.bill_date = billResponse.bill_date; bill.bill_updated_at = billResponse.bill_updated_at; bill.description = billResponse.description; bill.total_amount = billResponse.total_amount; bill.bill_updated_byId = billResponse.bill_updated_byId; _Context.bills.Add(bill); try { await _Context.SaveChangesAsync(); } catch (Exception exp) { _Logger.LogError($"Error in {nameof(AddNewBillAsync)}: " + exp.Message); } //int id = bill.billid; foreach (var x in billResponse.payers) { Payer payer = new Payer(); payer.billId = bill.billid; payer.paid_byId = x.paid_byId; payer.amount_paid = x.amount_paid; _Context.payers.Add(payer); try { await _Context.SaveChangesAsync(); } catch (Exception exp) { _Logger.LogError($"Error in {nameof(AddNewBillAsync)}: " + exp.Message); } } foreach (var x in billResponse.sharedwiths) { SharedWith sharedWith = new SharedWith(); sharedWith.billId = bill.billid; sharedWith.shared_withId = x.shared_withId; sharedWith.owes_amount = x.owes_amount; _Context.sharedWiths.Add(sharedWith); try { await _Context.SaveChangesAsync(); } catch (Exception exp) { _Logger.LogError($"Error in {nameof(AddNewBillAsync)}: " + exp.Message); } } foreach (var x in billResponse.settlements) { if (billResponse.groupId != 0) { Settlements settlements = new Settlements(); settlements = await _Context.settlements .SingleOrDefaultAsync(c => c.groupId == billResponse.groupId && c.payeeId == x.payeeId && c.payerId == x.payerId); if (settlements != null) { settlements.payerId = x.payerId; settlements.payeeId = x.payeeId; settlements.amount = settlements.amount + x.paid_amount; settlements.groupId = billResponse.groupId; _Context.settlements.Attach(settlements); _Context.Entry(settlements).State = EntityState.Modified; } else { settlements = await _Context.settlements .SingleOrDefaultAsync(c => c.groupId == billResponse.groupId && c.payeeId == x.payerId && c.payerId == x.payeeId); if (settlements != null) { if (settlements.amount > x.paid_amount) { settlements.payerId = x.payeeId; settlements.payeeId = x.payerId; settlements.groupId = billResponse.groupId; settlements.amount = settlements.amount - x.paid_amount; _Context.settlements.Attach(settlements); _Context.Entry(settlements).State = EntityState.Modified; } else { settlements.payerId = x.payerId; settlements.payeeId = x.payeeId; settlements.groupId = billResponse.groupId; settlements.amount = x.paid_amount - settlements.amount; _Context.settlements.Attach(settlements); _Context.Entry(settlements).State = EntityState.Modified; } } else { Settlements settlements1 = new Settlements(); settlements1.payerId = x.payerId; settlements1.payeeId = x.payeeId; settlements1.amount = x.paid_amount; settlements1.groupId = billResponse.groupId; _Context.settlements.Add(settlements1); } } } else { Settlements settlements = new Settlements(); settlements = await _Context.settlements .FirstOrDefaultAsync(c => c.groupId == bill.groupId && c.payeeId == x.payeeId && c.payerId == x.payerId); if (settlements != null) { settlements.payerId = x.payerId; settlements.payeeId = x.payeeId; settlements.amount = settlements.amount + x.paid_amount; _Context.settlements.Attach(settlements); _Context.Entry(settlements).State = EntityState.Modified; } else { settlements = await _Context.settlements .FirstOrDefaultAsync(c => c.groupId == bill.groupId && c.payeeId == x.payerId && c.payerId == x.payeeId); if (settlements != null) { if (settlements.amount > x.paid_amount) { settlements.payerId = x.payeeId; settlements.payeeId = x.payerId; settlements.amount = settlements.amount - x.paid_amount; _Context.settlements.Attach(settlements); _Context.Entry(settlements).State = EntityState.Modified; } else { settlements.payerId = x.payerId; settlements.payeeId = x.payeeId; settlements.amount = x.paid_amount - settlements.amount; _Context.settlements.Attach(settlements); _Context.Entry(settlements).State = EntityState.Modified; } } else { Settlements settlements1 = new Settlements(); settlements1.payerId = x.payerId; settlements1.payeeId = x.payeeId; settlements1.amount = x.paid_amount; // settlements1.groupId = billResponse.groupId; _Context.settlements.Add(settlements1); } } } try { await _Context.SaveChangesAsync(); } catch (Exception exp) { _Logger.LogError($"Error in {nameof(AddNewBillAsync)}: " + exp.Message); } } for (int i = 0; i < billResponse.sharedwiths.Count; i++) { for (int j = i + 1; j < billResponse.sharedwiths.Count; j++) { Friend friend = new Friend(); var frdexist = _Context.friends.SingleOrDefault( c => (c.userId == billResponse.sharedwiths[i].shared_withId && c.friendId == billResponse.sharedwiths[j].shared_withId) || (c.userId == billResponse.sharedwiths[j].shared_withId && c.friendId == billResponse.sharedwiths[i].shared_withId)); if (frdexist == null) { friend.friendId = billResponse.sharedwiths[j].shared_withId; friend.userId = billResponse.sharedwiths[i].shared_withId; _Context.friends.Add(friend); try { await _Context.SaveChangesAsync(); } catch (Exception exp) { _Logger.LogError($"Error in {nameof(AddNewBillAsync)}: " + exp.Message); } } } } return(bill); }
public async Task <GroupResponse> CreateGroupAsync(CreateGroupResponse group1) { GroupResponse response = new GroupResponse(); Group group = new Group(); group.group_name = group1.group_name; group.group_created_by = group1.group_created_by; group.is_simplified_depts = group1.is_simplified_depts; _Context.groups.Add(group); try { await _Context.SaveChangesAsync(); } catch (Exception exp) { _Logger.LogError($"Error in {nameof(CreateGroupAsync)}: " + exp.Message); } foreach (var x in group1.groupMembers) { GroupMember groupMember = new GroupMember(); groupMember.groupId = group.groupid; groupMember.userId = x.userId; _Context.groupMembers.Add(groupMember); try { await _Context.SaveChangesAsync(); } catch (Exception exp) { _Logger.LogError($"Error in {nameof(CreateGroupAsync)}: " + exp.Message); } } response = ModelData(group); for (int i = 0; i < group1.groupMembers.Count; i++) { for (int j = i + 1; j < group1.groupMembers.Count; j++) { Friend friend = new Friend(); var frdexist = _Context.friends.SingleOrDefault( c => (c.userId == group1.groupMembers[i].userId && c.friendId == group1.groupMembers[j].userId) || (c.userId == group1.groupMembers[j].userId && c.friendId == group1.groupMembers[i].userId)); if (frdexist == null) { friend.friendId = group1.groupMembers[j].userId; friend.userId = group1.groupMembers[i].userId; _Context.friends.Add(friend); try { await _Context.SaveChangesAsync(); } catch (Exception exp) { _Logger.LogError($"Error in {nameof(CreateGroupAsync)}: " + exp.Message); } } } } return(response); }