/// <summary> /// Create and store an instance of <see cref="User"/>. /// </summary> /// <param name="nickname">Nickname of the user.</param> /// <param name="username">A unique username of the user.</param> /// <param name="password">An unhashed password.</param> /// <param name="resolverContext">Resolver context used in error reporting.</param> /// <returns>An instance of proxy class <see cref="UserSignUpPayload"/> containing some of user fields.</returns> public UserSignUpPayload SignUp(string nickname, string username, string password, IResolverContext resolverContext) { if (nickname == null || username == null || password == null) { return(null); } if (_context.Users.Any(x => x.Username == username)) { resolverContext.ReportError(ErrorBuilder.New() .SetCode(UserSignUpErrorCode.UsernameNotUnique.ToString()) .SetMessage("Username is not unique!") .Build()); return(null); } var user = new User() { Nickname = nickname, Username = username, Password = _hasher.CreateHash(password) }; // Add and save user to generate Id _context.Add(user); _context.SaveChanges(); // Id and Username to ensures unique hash user.Token = _tokenizer.CreateToken(user.Id + ";" + user.Username); _context.Update(user); _context.SaveChanges(); return(new UserSignUpPayload() { Id = user.Id, Nickname = user.Nickname, Token = user.Token }); }
/// <summary> /// Attach mail to a mail group. /// </summary> /// <param name="mailId">Id of mail to attach.</param> /// <param name="mailGroupId">Id of group to attach to.</param> /// <returns>Associate entry of mail attached to mail group.</returns> public MailGroupMail AddMailToMailGroup(int mailId, int mailGroupId) { var mail = _context.Mails.SingleOrDefault(x => x.Id == mailId); var mailGroup = _context.MailGroups.SingleOrDefault(x => x.Id == mailGroupId); if (!isRequestValid(mail, mailGroup)) { return(null); } var mailGroupMail = new MailGroupMail() { MailId = mailId, MailGroupId = mailGroupId }; _context.Add(mailGroupMail); _context.SaveChanges(); return(mailGroupMail); }