Example #1
0
        public async Task <ActionResult <Data.Models.Author> > UpdateAuthor(int authorId, Data.Models.AuthorForUpdate updatedAuthor)
        {
            try
            {
                Data.Entities.Author dbAuthor = await _repository.GetAuthorAsync(authorId);

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

                _mapper.Map(updatedAuthor, dbAuthor);
                if (await _repository.SaveChangesAsync())
                {
                    Data.Models.Author savedAuthor = _mapper.Map <Data.Models.Author>(dbAuthor);
                    return(Ok(savedAuthor));
                }
                else
                {
                    return(BadRequest("Failed to update."));
                }
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Database exception: " + ex.Message));
            }
        }
Example #2
0
        public async Task <ActionResult <Data.Models.Author> > PatchAuthor(int authorId, JsonPatchDocument <Data.Models.AuthorForUpdate> patchDocument)
        {
            try
            {
                Data.Entities.Author dbAuthor = await _repository.GetAuthorAsync(authorId);

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

                var updatedAuthor = _mapper.Map <Data.Models.AuthorForUpdate>(dbAuthor);
                patchDocument.ApplyTo(updatedAuthor, ModelState);

                _mapper.Map(updatedAuthor, dbAuthor);

                if (await _repository.SaveChangesAsync())
                {
                    Data.Models.Author savedAuthor = _mapper.Map <Data.Models.Author>(await _repository.GetAuthorAsync(authorId));
                    return(Ok(savedAuthor));
                }
                else
                {
                    return(StatusCode(StatusCodes.Status500InternalServerError, "Unable to save to database"));
                }
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Unable to patch author " + ex.Message));
            }
        }
Example #3
0
        public async Task <ActionResult <Data.Models.Author> > CreateNewAuthor(Data.Models.AuthorForCreate newAuthor)
        {
            Data.Entities.Author dbNewAuthor = null;
            try
            {
                dbNewAuthor = _mapper.Map <Data.Entities.Author>(newAuthor);
            }
            catch (Exception ex)
            {
                return(BadRequest("Input is in invalid format: " + ex.Message));
            }

            if (dbNewAuthor == null)
            {
                return(BadRequest("Input is in invalid format"));
            }

            await _repository.AddAsync <Data.Entities.Author>(dbNewAuthor);

            await _repository.SaveChangesAsync();

            Data.Models.Author addedAuthor = _mapper.Map <Data.Models.Author>(dbNewAuthor);

            var url = _linkgenerator.GetPathByAction(HttpContext, "GetAuthorByAuthorId", "Authors", addedAuthor);

            return(this.Created(url, addedAuthor));
        }