Exemple #1
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));
            }
        }
Exemple #2
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));
        }
Exemple #3
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));
            }
        }
        public async Task <Data.Entities.Author> PostAuthor(Data.Entities.Author author)
        {
            _context.Authors.Add(author);
            await _context.SaveChangesAsync();

            return(author);
        }
        public async Task <Data.Entities.Author> PutAuthor(int id, Data.Entities.Author author)
        {
            _context.Entry(author).State = EntityState.Modified;
            await _context.SaveChangesAsync();

            return(author);
        }
Exemple #6
0
        public async Task <Models.Author> PutAuthor(int id, Models.Author author)
        {
            Data.Entities.Author authorEnt    = _mapper.Map <Data.Entities.Author>(author);
            Data.Entities.Author authorReturn = await _authorRepository.PutAuthor(id, authorEnt);

            return(_mapper.Map <Models.Author>(authorReturn));
        }
Exemple #7
0
        public async Task <Models.Author> PostAuthor(Models.Author author)
        {
            _logger.LogInformation($" ***** Inicia.");
            Data.Entities.Author authorEnt = _mapper.Map <Data.Entities.Author>(author);
            var result = await _authorRepository.PostAuthor(authorEnt);

            Models.Author authorModel = _mapper.Map <Models.Author>(result);
            return(authorModel);
        }
Exemple #8
0
 public static Author DtoS(Data.Entities.Author author)
 {
     return(new Author
     {
         Id = author.Id,
         FirstName = author.FirstName,
         LastName = author.LastName,
         Description = author.Description,
         Address = author.Address,
         Email = author.Email
     });
 }
Exemple #9
0
        public async Task <IActionResult> DeleteAuthor(int authorId)
        {
            try
            {
                Data.Entities.Author dbAuthor = await _repository.GetAuthorAsync(authorId);

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

                _repository.Delete <Data.Entities.Author>(dbAuthor);
                await _repository.SaveChangesAsync();

                return(NoContent());
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Database exception: " + ex.Message));
            }
        }