public async Task <ActionResult <bool> > AddAsFriend(Friends request) { try { var allFriendships = await _context.Friends.ToListAsync(); var currentFriendship = allFriendships.Where(a => a.FriendOneId == request.FriendOneId && a.FriendTwoId == request.FriendTwoId).FirstOrDefault(); if (currentFriendship == null) { Friends newFriendship = new Friends(); newFriendship.FriendOneId = request.FriendOneId; newFriendship.FriendTwoId = request.FriendTwoId; _context.Friends.Add(newFriendship); int success = _context.SaveChanges(); if (success > 0) { return(true); } else { return(false); } } else { return(false); } } catch (Exception ex) { Console.WriteLine(ex); return(false); } }
public async Task <ActionResult <bool> > registerUser(Users user) { try { var allUsers = await _context.Users.ToListAsync(); var currentUser = allUsers.Where(a => a.Email == user.Email).FirstOrDefault(); if (currentUser == null) { Users newUser = new Users(); newUser.Email = user.Email; newUser.Username = user.Username; newUser.Password = user.Password; _context.Users.Add(newUser); int success = _context.SaveChanges(); if (success > 0) { return(true); } else { return(false); } } else { return(false); } } catch (Exception ex) { Console.WriteLine(ex); return(false); } }
public async Task <ActionResult <bool> > SaveMessages(Message message) { try { var msg = new Message(); msg.SenderId = message.SenderId; msg.ReceiverId = message.ReceiverId; msg.Messagebody = message.Messagebody; _context.Message.Add(msg); int success = _context.SaveChanges(); return(true); } catch (Exception ex) { Console.WriteLine(ex); return(null); } }