Exemplo n.º 1
0
        public async Task <IActionResult> PostAsync([FromBody] SaveSubscribeResource resource)
        {
            // validating the request data
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState.GetErrorMessages()));
            }

            resource.DataStart = DateTime.Now;
            resource.DataEnd   = resource.DataStart.AddYears(1);

            //  mapping the resource to our model
            var subscribe = _mapper.Map <SaveSubscribeResource, Subscribe>(resource);

            // get result from model (return response)
            var result = await _subscribeService.SaveAsync(subscribe);

            // API returns a bad request
            if (!result.Success)
            {
                return(BadRequest(result.Message));
            }

            // API maps the new category (now including data such as the new Id) to our previously created SubscribeResource
            // get subscribe from response
            var subscribeResource = _mapper.Map <Subscribe, SubscribeResource>(result.Subscribe);

            // sends it to the client
            return(Ok(subscribeResource));
        }