private ActionResult ExecuteCommandHandler(CancelPurchaseApplicationCommand command)
 {
     return(commandHandler
            .Cancel(command)
            .Match(
                Left: error => BadRequest(BadRequestResponseModel.CreateOperationErrorResponse(error.ToString())),
                Right: _ => (ActionResult)Ok()));
 }
        private ActionResult BuildValidationErrorResponse(Seq <ValidationError <GenericValidationErrorCode> > errors)
        {
            var validationErrors = errors.Map(error =>
                                              new ValidationError(
                                                  fieldId: error.FieldId,
                                                  errorCode: error.ErrorCode.ToString()))
                                   .ToList();

            return(BadRequest(BadRequestResponseModel.CreateValidationErrorResponse(validationErrors)));
        }
示例#3
0
        public async Task <IActionResult> Register([FromBody] RegisterModel model)
        {
            try
            {
                if (string.IsNullOrEmpty(model.Username) ||
                    string.IsNullOrEmpty(model.FirstName) ||
                    string.IsNullOrEmpty(model.LastName) ||
                    string.IsNullOrEmpty(model.Email) ||
                    string.IsNullOrEmpty(model.Password))
                {
                    var badRequestResponse = new BadRequestResponseModel()
                    {
                        Message     = "All fields are required.",
                        Description = "All fields are required.",
                        Details     = new List <string>()
                    };
                    return(StatusCode(400, badRequestResponse));
                }

                var user = await _applicationDbContext.User.FirstOrDefaultAsync(x => x.Username.ToLower() == model.Username.ToLower());

                if (user != null)
                {
                    var badRequestResponse = new BadRequestResponseModel()
                    {
                        Message     = "Username is taken. Choose another username.",
                        Description = "Username is taken. Choose another username.",
                        Details     = new List <string>()
                    };
                    return(StatusCode(400, badRequestResponse));
                }

                user          = new User();
                user.Id       = 0;
                user.Username = model.Username;
                user.Password = Utilities.GetSHA256(model.Password);
                user.Name     = model.FirstName;
                user.Surname  = model.Username;
                user.Email    = model.Email;
                _applicationDbContext.Add(user);
                await _applicationDbContext.SaveChangesAsync();

                return(StatusCode(200));
            }
            catch (Exception exception)
            {
                var errorResponse = new InternalServerErrorResponseModel();
                errorResponse.LogId      = Guid.NewGuid().ToString();
                errorResponse.Message    = exception.Message;
                errorResponse.StackTrace = exception.StackTrace;
                return(StatusCode(500, errorResponse));
            }
        }