public IActionResult Create([FromBody] TrainingDayViewModel viewModel) { try { if (ModelState.IsValid) { if (string.IsNullOrWhiteSpace(viewModel.UserId) || viewModel.Year == 0 || viewModel.WeekOfYear == 0 || viewModel.DayOfWeek < 0 || viewModel.DayOfWeek > 6 || SessionUserId != viewModel.UserId) { return(BadRequest()); } //Verify trainingWeek exist var trainingWeekKey = new TrainingWeekKey() { UserId = viewModel.UserId, Year = viewModel.Year, WeekOfYear = viewModel.WeekOfYear }; var trainingWeekScenario = new TrainingWeekScenario() { ManageTrainingDay = false }; var trainingWeek = _trainingWeeksService.GetTrainingWeek(trainingWeekKey, trainingWeekScenario); if (trainingWeek == null) { return(BadRequest(new WebApiException(string.Format(Translation.P0_NOT_EXIST, Translation.TRAINING_WEEK)))); } //Verify valid week of year if (viewModel.WeekOfYear > 0 && viewModel.WeekOfYear <= 52) { var trainingDay = ControllerUtils.TransformViewModelToTrainingDay(viewModel); trainingDay = _trainingDaysService.CreateTrainingDay(trainingDay); if (trainingDay == null) { return(BadRequest(new WebApiException(string.Format(Translation.IMPOSSIBLE_TO_CREATE_P0, Translation.TRAINING_DAY)))); } else { return(new OkObjectResult(trainingDay)); } } else { return(BadRequest()); } } else { return(BadRequest(new WebApiException(ControllerUtils.GetModelStateError(ModelState)))); } } catch (Exception exception) { return(BadRequest(new WebApiException("Error", exception))); } }
public async Task <IActionResult> Register([FromBody] RegisterAccountViewModel registerAccount) { if (registerAccount == null || string.IsNullOrWhiteSpace(registerAccount.UserName) || string.IsNullOrWhiteSpace(registerAccount.Email) || string.IsNullOrWhiteSpace(registerAccount.Password)) { return(BadRequest()); } if (ModelState.IsValid) { var mailUser = await _identityUserManager.FindByEmailAsync(registerAccount.Email); if (mailUser != null) { return(BadRequest(new WebApiException("Email already exist"))); } var user = new ApplicationUser { UserName = registerAccount.UserName, Email = registerAccount.Email }; var result = await _identityUserManager.CreateAsync(user, registerAccount.Password); if (result.Succeeded) { string reportData; user.RegistrationDate = DateTime.Now; await _identityUserManager.UpdateAsync(user); _logger.LogInformation(3, "User created a new account with password."); try { // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=532713 // Send an email with this link var code = await _identityUserManager.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = Url.Action("ConfirmEmail", "Account", new { area = "Site", userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme); reportData = await _reportService.CreateReportForConfirmUserAccountAsync(user.Id, callbackUrl); await _emailSender.SendEmailAsync(registerAccount.Email, Translation.CONFIRM_USER_ACCOUNT, reportData); //await _signInManager.SignInAsync(user, isPersistent: false); } catch (Exception except) { _logger.LogError(3, except, "can't send email "); } //SendEmail to admin reportData = await _reportService.CreateReportForAdminNewUserAccountCreatedAsync(user.Id); await ControllerUtils.SendEmailToAdminAsync(_usersService, _emailSender, "Nouvel utilisateur mobile", reportData); return(new OkObjectResult(true)); } StringBuilder errorMessage = new StringBuilder(); foreach (var error in result.Errors) { errorMessage.AppendLine(error.Description); } return(BadRequest(new WebApiException(errorMessage.ToString()))); } else { return(BadRequest(new WebApiException(ControllerUtils.GetModelStateError(ModelState)))); } }