public async Task <ControllerResult> SignUp(SignUp model) { if (!ModelState.IsValid) { string errorMessage = string.Empty; foreach (ModelState modelState in ModelState.Values) { foreach (ModelError error in modelState.Errors) { errorMessage = string.Format("{0}\n{1}", errorMessage, error.ErrorMessage); } } ControllerResult responce = new ControllerResult() { IsSucces = true, Message = string.Format("{0}/#/SignUp/{1}", Repository.DNS, errorMessage) }; return(responce); } NotActiveUser user = new NotActiveUser(model); RepositoryResult <ControllerResult> result = await this.repository.RegistartionAsync(user); return(result.Responce); }
public async Task <RepositoryResult <User, HttpResponseMessage> > Activation(Guid id) { RepositoryResult <User, HttpResponseMessage> result = new RepositoryResult <User, HttpResponseMessage>(); NotActiveUser notActiveUser = this.context.Get <NotActiveUser, Guid>(id); if (notActiveUser == null) { HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.Moved); response.Headers.Location = new Uri(base.MovedError(404, "Пользователь не найден")); result.Responce = response; result.ResultType = RepositoryResultType.Bad; logger.WriteError(string.Format("Не активированный пользователь с id = {0} не найден")); } else { User newUser = new User(notActiveUser); this.context.Delete(notActiveUser); await this.context.SaveChangesAsync(); this.logger.WriteInformation(string.Format("Пользователь с id = {0} и ФИО = {1} успешно активировался, теперь id = {2}", notActiveUser.Id, newUser.FullName, newUser.Id)); HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.Moved); response.Headers.Location = new Uri(string.Format("{0}/#/SignIn", DNS)); result.Responce = response; result.Value = newUser; result.ResultType = RepositoryResultType.OK; } return(result); }
public TEntity Get <TEntity, TId>(TId idEntity) where TEntity : class { Type typeEntity = typeof(TEntity); Type typeId = typeof(TId); if (typeEntity == typeof(User)) { if (typeId == typeof(string)) { string id = idEntity as string; User user = this.GetAll <User>().FirstOrDefault(u => u.Id == id); if (user == null) { this.logger.WriteError(string.Format("Активиованный пользователь с id = {0} не найден", id)); return(null); } else { return(user as TEntity); } } else { throw new ArgumentException(string.Format("Type {0} of id for {1} is not valid. Type of Id {2} for {1} is valid" , typeId , typeof(User) , typeof(string))); } } else if (typeEntity == typeof(NotActiveUser)) { if (typeId == typeof(Guid)) { Guid id = (Guid)(object)idEntity; NotActiveUser user = this.GetAll <NotActiveUser>().FirstOrDefault(u => u.Id == id); if (user == null) { this.logger.WriteError(string.Format("Не активиованный пользователь с id = {0} не найден", id)); return(null); } else { return(user as TEntity); } } else { throw new ArgumentException(string.Format("Type {0} of id for {1} is not valid. Type of Id {2} for {1} is valid" , typeId , typeof(NotActiveUser) , typeof(Guid))); } } else if (typeEntity == typeof(Salt)) { if (typeId == typeof(Guid)) { Guid id = (Guid)(object)idEntity; Salt salt = this.GetAll <Salt>().FirstOrDefault(s => s.Id == id); if (salt == null) { this.logger.WriteError(string.Format("Соль пароля с id = {0} не найден", id)); return(null); } else { return(salt as TEntity); } } else { throw new ArgumentException(string.Format("Type {0} of id for {1} is not valid. Type of Id {2} for {1} is valid" , typeId , typeof(Salt) , typeof(Guid))); } } else if (typeEntity == typeof(News)) { if (typeId == typeof(Guid)) { Guid id = (Guid)(object)idEntity; News news = this.GetAll <News>().FirstOrDefault(n => n.Id == id); if (news == null) { this.logger.WriteError(string.Format("Новость с id = {0} не найден", id)); return(null); } else { return(news as TEntity); } } else { throw new ArgumentException(string.Format("Type {0} of id for {1} is not valid. Type of Id {2} for {1} is valid" , typeId , typeof(News) , typeof(Guid))); } } else if (typeEntity == typeof(Image)) { if (typeId == typeof(Guid)) { Guid id = (Guid)(object)idEntity; Image image = this.GetAll <Image>().FirstOrDefault(i => i.Id == id); if (image == null) { this.logger.WriteError(string.Format("Изображение с id = {0} не найден", id)); return(null); } else { return(image as TEntity); } } else { throw new ArgumentException(string.Format("Type {0} of id for {1} is not valid. Type of Id {2} for {1} is valid" , typeId , typeof(Image) , typeof(Guid))); } } else if (typeEntity == typeof(Resolution)) { if (typeId == typeof(Guid)) { Guid id = (Guid)(object)idEntity; Resolution resolution = this.GetAll <Resolution>().FirstOrDefault(r => r.Id == id); if (resolution == null) { this.logger.WriteError(string.Format("Разрешение с id = {0} не найден", id)); return(null); } else { return(resolution as TEntity); } } else { throw new ArgumentException(string.Format("Type {0} of id for {1} is not valid. Type of Id {2} for {1} is valid" , typeId , typeof(Resolution) , typeof(Guid))); } } else { throw new ArgumentException(string.Format("Entity with type {0} not a found.", typeEntity)); } }
public async Task <RepositoryResult <ControllerResult> > RegistartionAsync(NotActiveUser user) { RepositoryResult <ControllerResult> result = new RepositoryResult <ControllerResult>(); NotActiveUser isHaveUser = context.GetAll <NotActiveUser>().FirstOrDefault((u) => u.Email == user.Email); User isHaveActiveUser = context.GetAll <User>().FirstOrDefault((u) => u.Email == user.Email); if (isHaveUser != null || isHaveActiveUser != null) { result.ResultType = RepositoryResultType.Bad; result.Responce = new ControllerResult() { IsSucces = false, Message = string.Format("Почта {0} уже занята.", user.Email) }; return(result); } try { string href = string.Format("{0}/api/User/Activation?id={1}", DNS, user.Id); base.SendMessage( user.Email, "Solo-grupp подтверждение аккаунта", string.Format("<a href=\"{0}\">{0}</a>", href), true); this.logger.WriteInformation(string.Format("Зарегестрировался новый пользователь id = {0}. Письмо подтверждения отправлено на {1}.", user.Id, user.Email)); result.ResultType = RepositoryResultType.OK; result.Responce = new ControllerResult() { IsSucces = true, Message = string.Format("Письмо с подтверждением отправлено на {0}.", user.Email) }; context.Add(user); int changes = await context.SaveChangesAsync(); if (changes == 0) { result.ResultType = RepositoryResultType.Bad; result.Responce = new ControllerResult() { IsSucces = false, Message = "Ошибка на серевере. Сервер временно недоступен, приносим свои извинения." }; return(result); } } catch (Exception ex) { this.logger.WriteError(ex, string.Format("Ошибка при отправке сообщения на {0}.", user.Email)); result.ResultType = RepositoryResultType.Bad; result.Responce = new ControllerResult() { IsSucces = false, Message = string.Format("Не удалось отправить письмо на {0}.", user.Email) }; } return(result); }