コード例 #1
0
        public async Task <ActionResult <PublisherCreateResource> > PutPublihser(int id, [FromBody] PublisherModel publisherModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            // You can make it like Yazan said from his document.
            var publisherToUpdate = await _publisherRepository.Get(id);

            if (publisherToUpdate == null)
            {
                return(NotFound());
            }

            publisherToUpdate.Name = publisherModel.Name;

            await _publisherRepository.Update(publisherToUpdate);

            var publisherResource = new PublisherCreateResource
            {
                Id   = publisherToUpdate.Id,
                Name = publisherToUpdate.Name,
            };

            return(Ok(publisherResource));
        }
コード例 #2
0
        public async Task <ActionResult <PublisherCreateResource> > PostPubliser([FromBody] PublisherModel publisherModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            // Here map (model) -> entity
            var publisherEntity = new Publisher
            {
                Name = publisherModel.Name,
            };
            // Entity from Book
            var newPublisher = await _publisherRepository.Create(publisherEntity);

            // Here map (newBook which is Entity) -> Resource
            var publisherResource = new PublisherCreateResource
            {
                Id   = newPublisher.Id,
                Name = newPublisher.Name,
            };


            return(CreatedAtAction(nameof(GetPublishers), new { id = newPublisher.Id }, publisherResource));
        }