public void AddComment(int id, Comment comment, User sender)
 {
     var request = dbContext.HelpRequests.Find(id);
     var user = dbContext.Users.Attach(sender);
     comment.User = user;
     request.Comments.Add(comment);
     dbContext.SaveChanges();
 }
        public void MarkSolved(int requestId, User user)
        {
            var request = dbContext.HelpRequests.Find(requestId);
            if(request.User.Id != user.Id)
            {
                throw new Exception("Invalid user");
            }

            request.Solved = true;
            dbContext.SaveChanges();
        }
 public static UserModel FromUser(User user)
 {
     return new UserModel()
                {
                    Id = user.Id,
                    FirstName = user.FirstName,
                    LastName = user.LastName,
                    Username = user.Username,
                    ProfilePictureUrl = user.ProfilePictureUrl
                };
 }
        public bool SendContactRequest(User sender, User receiver)
        {
            dbContext.Users.Attach(sender);
            dbContext.Users.Attach(receiver);

            if (receiver.FriendRequests.Any(c => c.Sender.Id == sender.Id))
            {
                return false;
            }

            receiver.FriendRequests.Add(new FriendRequest() { Sender = sender });
            dbContext.SaveChanges();
            return true;
        }
        public bool AcceptContactRequest(int requestId, User user)
        {
            var request = dbContext.FriendRequests.FirstOrDefault(c => c.Id == requestId);
            if (request == null)
            {
                return false;
            }

            dbContext.Users.Attach(user);
            if (!user.FriendRequests.Any(c => c.Id == requestId))
            {
                return false;
            }

            user.Friends.Add(request.Sender);
            request.Sender.Friends.Add(user);
            dbContext.SaveChanges();

            dbContext.FriendRequests.Remove(request);
            dbContext.SaveChanges();

            return true;
        }
 public IEnumerable<HelpRequest> GetByUser(User sender)
 {
     return dbContext.HelpRequests.Where(r => r.User.Id == sender.Id).ToList();
 }
 public void Add(User sender, HelpRequest request)
 {
     var user = dbContext.Users.Attach(sender);
     user.HelpRequests.Add(request);
     dbContext.SaveChanges();
 }
 public void Logout(User user)
 {
     dbContext.Users.Attach(user);
     user.SessionKey = null;
     dbContext.SaveChanges();
 }
 public void Add(User user)
 {
     dbContext.Users.Add(user);
     dbContext.SaveChanges();
 }
 public void SetSessionKey(User user, string sessionKey)
 {
     dbContext.Users.Attach(user);
     user.SessionKey = sessionKey;
     dbContext.SaveChanges();
 }
        public bool EditUser(User userToEdit, string newPasswordHash)
        {
            var user = dbContext.Users.Find(userToEdit.Id);
            if (userToEdit.PasswordHash != null)
            {
                if (user.Username != userToEdit.Username || user.PasswordHash != userToEdit.PasswordHash)
                {
                    return false;
                }
            }

            user.FirstName = userToEdit.FirstName ?? user.FirstName;
            user.LastName = userToEdit.LastName ?? user.LastName;
            user.PasswordHash = (userToEdit.PasswordHash != null) ? newPasswordHash : user.PasswordHash;
            user.ProfilePictureUrl = userToEdit.ProfilePictureUrl ?? user.ProfilePictureUrl;
            dbContext.SaveChanges();
            return true;
        }