public async Task <IActionResult> PostRequestAction([FromBody] RequestAction requestAction)
        {
            string  sRequestStatus = null;
            Request request;

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!UsertExists(requestAction.Username))
            {
                return(BadRequest("User does not exist."));
            }

            if (!RequestExists(requestAction.RequestId))
            {
                return(BadRequest("Request does not exist."));
            }

            if (!RequestActions.IsValidAction(requestAction.Action))
            {
                return(BadRequest("Request Action does not exist."));
            }

            if (requestAction.Action == RequestActions.DB_APPROVE)
            {
                sRequestStatus = RequestStates.DB_APPROVED;
            }
            else if (requestAction.Action == RequestActions.DB_REJECT)
            {
                sRequestStatus = RequestStates.DB_REJECED;
            }

            _context.RequestActions.Add(requestAction);

            request = await _context.Requests.FindAsync(requestAction.RequestId);

            if (sRequestStatus != null)
            {
                request.State = sRequestStatus;
                _context.Requests.Update(request);
            }

            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetRequestAction", new { id = requestAction.RequestId }, requestAction));
        }
Example #2
0
        public async Task <TResponse> Handle(TRequest request, CancellationToken cancellationToken)
        {
            if (!RequestActions.TryGetValue(request.Action, out var requestAction))
            {
                throw new NotSupportedException($"Unable to find an action for {request.Action}");
            }

            if (RequestValidation.TryGetValue(request.Action, out var requestValidationDelegate))
            {
                var attempt = await requestValidationDelegate.Invoke(request, cancellationToken);

                if (!attempt.Successful)
                {
                    if (attempt.ValidationFailures != null && attempt.ValidationFailures.Any())
                    {
                        throw new ValidationException(attempt.ValidationFailures);
                    }

                    throw new ArgumentException("Request validation failed", nameof(request));
                }
            }

            return(await requestAction?.Invoke(request, cancellationToken));
        }