Ejemplo n.º 1
0
        public async Task <ActionResult> CustomerMove(int key, [SingleParameterBinder(typeof(NewAddress))] NewAddress newAddress)
        {
            if (newAddress == null)
            {
                return(this.Problem(ProblemJsonBuilder.CreateBadParameters()));
            }

            try
            {
                var customer = await customerRepository.GetEnitityByKeyAsync(key);

                var hypermediaCustomer = new HypermediaCustomer(customer);
                hypermediaCustomer.MoveAction.Execute(newAddress);
                return(Ok());
            }
            catch (EntityNotFoundException)
            {
                return(this.Problem(ProblemJsonBuilder.CreateEntityNotFound()));
            }
            catch (CanNotExecuteActionException)
            {
                return(this.CanNotExecute());
            }
            catch (ActionParameterValidationException e)
            {
                var problem = new ProblemJson()
                {
                    Title       = $"Can not use provided object of type '{typeof(NewAddress)}'",
                    Detail      = e.Message,
                    ProblemType = "WebApiHypermediaExtensionsCore.Hypermedia.BadActionParameter",
                    StatusCode  = 422 // Unprocessable Entity
                };
                return(this.UnprocessableEntity(problem));
            }
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> BuyCar(int key, BuyCarParameters parameter)
        {
            if (parameter == null)
            {
                var problem = new ProblemJson
                {
                    Title       = $"Can not use provided object of type '{typeof(BuyCarParameters)}'",
                    Detail      = "Json or contained links might be invalid",
                    ProblemType = "WebApi.HypermediaExtensions.Hypermedia.BadActionParameter",
                    StatusCode  = 422 // Unprocessable Entity
                };
                return(this.UnprocessableEntity(problem));
            }

            try
            {
                //shortcut for get car from repository
                var car      = new HypermediaCarHto(parameter.Brand, parameter.CarId);
                var customer = await customerRepository.GetEnitityByKeyAsync(key).ConfigureAwait(false);

                //do what has to be done
                return(Ok());
            }
            catch (EntityNotFoundException)
            {
                return(this.Problem(ProblemJsonBuilder.CreateEntityNotFound()));
            }
            catch (CanNotExecuteActionException)
            {
                return(this.CanNotExecute());
            }
        }
Ejemplo n.º 3
0
 public async Task <ActionResult> RemoveCustomer(int key)
 {
     try
     {
         return(Ok());
     }
     catch (EntityNotFoundException)
     {
         return(this.Problem(ProblemJsonBuilder.CreateEntityNotFound()));
     }
     catch (CanNotExecuteActionException)
     {
         return(this.CanNotExecute());
     }
     catch (ActionParameterValidationException e)
     {
         var problem = new ProblemJson()
         {
             Title       = $"Can not use provided object of type '{typeof(NewAddress)}'",
             Detail      = e.Message,
             ProblemType = "WebApi.HypermediaExtensions.Hypermedia.BadActionParameter",
             StatusCode  = 422 // Unprocessable Entity
         };
         return(this.UnprocessableEntity(problem));
     }
 }
Ejemplo n.º 4
0
        public async Task <ActionResult> NewCustomerAction([SingleParameterBinder(typeof(CreateCustomerParameters))] CreateCustomerParameters createCustomerParameters)
        {
            if (createCustomerParameters == null)
            {
                return(this.Problem(ProblemJsonBuilder.CreateBadParameters()));
            }

            var createdCustomer = await customersRoot.CreateCustomerAction.Execute(createCustomerParameters);

            // Will create a Location header with a URI to the result.
            return(this.Created(new HypermediaCustomer(createdCustomer)));
        }
Ejemplo n.º 5
0
        public async Task <ActionResult> NewCustomerAction(CreateCustomerParameters createCustomerParameters)
        {
            if (createCustomerParameters == null)
            {
                return(this.Problem(ProblemJsonBuilder.CreateBadParameters()));
            }

            var createdCustomer = await customersRoot.CreateCustomer.ExecuteAsync(createCustomerParameters).ConfigureAwait(false);

            // Will create a Location header with a URI to the result.
            return(this.Created(createdCustomer));
        }
Ejemplo n.º 6
0
 public ActionResult GetEntity(string brand, int key)
 {
     try
     {
         // short cut for example, we should actually call the Car repo and get a Car domain object
         var result = new HypermediaCar(brand, key);
         return(Ok(result));
     }
     catch (EntityNotFoundException)
     {
         return(this.Problem(ProblemJsonBuilder.CreateEntityNotFound()));
     }
 }
Ejemplo n.º 7
0
        public async Task <ActionResult> GetEntity(int key)
        {
            try
            {
                var customer = await customerRepository.GetEnitityByKeyAsync(key);

                var result = new HypermediaCustomer(customer);
                return(Ok(result));
            }
            catch (EntityNotFoundException)
            {
                return(this.Problem(ProblemJsonBuilder.CreateEntityNotFound()));
            }
        }
Ejemplo n.º 8
0
        public ActionResult NewQueryAction([SingleParameterBinder(typeof(CustomerQuery))] CustomerQuery query)
        {
            if (query == null)
            {
                return(this.Problem(ProblemJsonBuilder.CreateBadParameters()));
            }

            if (!customersRoot.CreateQueryAction.CanExecute())
            {
                return(this.CanNotExecute());
            }

            // Will create a Location header with a URI to the result.
            return(this.CreatedQuery(typeof(HypermediaCustomerQueryResult), query));
        }
Ejemplo n.º 9
0
        public async Task <ActionResult> MarkAsFovoriteAction(int key)
        {
            try
            {
                var customer = await customerRepository.GetEnitityByKeyAsync(key);

                var hypermediaCustomer = new HypermediaCustomer(customer);
                hypermediaCustomer.MarkAsFavoriteAction.Execute();
                return(Ok());
            }
            catch (EntityNotFoundException)
            {
                return(this.Problem(ProblemJsonBuilder.CreateEntityNotFound()));
            }
            catch (CanNotExecuteActionException)
            {
                return(this.CanNotExecute());
            }
        }
Ejemplo n.º 10
0
        public async Task <ActionResult> MarkAsFavoriteAction([HypermediaActionParameterFromBody] MarkAsFavoriteParameters favoriteCustomer)
        {
            if (favoriteCustomer == null)
            {
                var problem = new ProblemJson
                {
                    Title       = $"Can not use provided object of type '{typeof(MarkAsFavoriteParameters)}'",
                    Detail      = "Json or contained links might be invalid",
                    ProblemType = "WebApi.HypermediaExtensions.Hypermedia.BadActionParameter",
                    StatusCode  = 422 // Unprocessable Entity
                };
                return(this.UnprocessableEntity(problem));
            }

            try
            {
                var customer = await customerRepository.GetEnitityByKeyAsync(favoriteCustomer.CustomerId).ConfigureAwait(false);

                var hypermediaCustomer = customer.ToHto();
                hypermediaCustomer.MarkAsFavorite.Execute(favoriteCustomer);
                return(Ok());
            }
            catch (EntityNotFoundException)
            {
                return(this.Problem(ProblemJsonBuilder.CreateEntityNotFound()));
            }
            catch (InvalidLinkException e)
            {
                var problem = new ProblemJson()
                {
                    Title       = $"Can not use provided object of type '{typeof(MarkAsFavoriteParameters)}'",
                    Detail      = e.Message,
                    ProblemType = "WebApi.HypermediaExtensions.Hypermedia.BadActionParameter",
                    StatusCode  = 422 // Unprocessable Entity
                };
                return(this.UnprocessableEntity(problem));
            }
            catch (CanNotExecuteActionException)
            {
                return(this.CanNotExecute());
            }
        }
Ejemplo n.º 11
0
        public async Task <ActionResult> Query([FromQuery] CustomerQuery query)
        {
            if (query == null)
            {
                return(this.Problem(ProblemJsonBuilder.CreateBadParameters()));
            }

            var queryResult = await customerRepository.QueryAsync(query);

            var resultReferences = new List <HypermediaObjectReferenceBase>();

            foreach (var customer in queryResult.Entities)
            {
                resultReferences.Add(new HypermediaObjectReference(new HypermediaCustomer(customer)));
            }

            var result           = new HypermediaCustomerQueryResult(resultReferences, queryResult.TotalCountOfEnties, query);
            var navigationQuerys = NavigationQuerysBuilder.Build(query, queryResult);

            result.AddNavigationQueries(navigationQuerys);

            return(Ok(result));
        }