public async Task <APIResult> Create(TaskEditGrantsBL taskEditGrantRequest) { APIResult res = CheckTask(taskEditGrantRequest, true, out TaskEditGrants taskGrant); if (!res.Success) { return(res); } var model = new TaskEditGrants() { TaskId = taskEditGrantRequest.TaskId, FriendId = taskEditGrantRequest.FriendId, date = taskEditGrantRequest.date, IsGranted = false }; context.TaskEditGrants.Create(model); await context.SaveChangesAsync(); var token = await _tokenService.GetIdentityToken(new LoginViewModel() { UserName = "******", Password = "******" }); if (!token.Success) { throw new Exception(token.Error); } var UserId = context.UsersInTasks.GetAll().Where(t => t.MyTaskId == model.TaskId && t.UserInTaskTypeCode == 1).FirstOrDefault().UserId; var friend = (await _client.GetWithToken <IEnumerable <UserFriendBL> >($"api/friendsforuser/{taskEditGrantRequest.FriendId}", token.token)).FirstOrDefault(u => u.FriendId == UserId); if (friend != null) { await _mailService.SendAsync(new MailModelBL() { To = friend.Friend.Email, Subject = "Запрос на редактирование задачи", Body = "Вам пришёл запрос на редактирование задачи <br> Чтобы принять или отклонить запрос перейдите по ссылке: <br> <a href=\"https://localhost:44347/friends\">Перейти...</a>" }); } return(res); }
private APIResult CheckTask(TaskEditGrantsBL taskEditGrantRequest, bool oncreate, out TaskEditGrants taskGrant) { taskGrant = null; int TaskId = taskEditGrantRequest.TaskId; MyTask task = context.MyTasks.GetById(TaskId); if (task == null) { return new APIResult() { Success = false, Error = "Неправильный TaskId" } } ; string FriendId = taskEditGrantRequest.FriendId; if (!context.UsersInTasks.GetAll().Any(ut => ut.UserId == FriendId && ut.MyTaskId == TaskId && ut.UserInTaskTypeCode == 2)) { return new APIResult() { Success = false, Error = "Неправильный FriendId" } } ; taskGrant = context.TaskEditGrants.GetAll().FirstOrDefault(teg => teg.FriendId == FriendId && teg.TaskId == TaskId); if (taskGrant == null && !oncreate) { return new APIResult() { Success = false, Error = "Такого запроса не существует" } } ; if (taskGrant != null && oncreate) { return new APIResult() { Success = false, Error = "Такой запрос уже существует" } } ; return(new APIResult() { Success = true }); }