public void UpdateFromAPI(long id, AuthorUpdate authorUpdate)
        {
            AuthorRepository authorRepository = new AuthorRepository();

            authorRepository.Name = authorUpdate.Name;
            this.authorStoreRepository.Update(id, authorRepository);
        }
Beispiel #2
0
        public IActionResult ParcialUpdateAuthor(Guid id,
                                                 [FromBody] JsonPatchDocument <AuthorUpdate> auth)
        {
            if (auth == null)
            {
                return(BadRequest());
            }
            var authore = Repo.GetAuthor(id, true);

            //upserting with patch
            if (authore == null)
            {
                var author = new AuthorUpdate();
                auth.ApplyTo(author, ModelState);
                if (author.FirstName == author.LastName)
                {
                    ModelState.AddModelError(nameof(AuthorUpdate),
                                             "the name and lastname cannot be the same");
                }
                TryValidateModel(author);
                if (!ModelState.IsValid)
                {
                    return(new UnProccessableObjectResult(ModelState));
                }
                var authToAdd = Mapper.Map <AuthorUpdate, Author>(author);
                Repo.AddAuthor(authToAdd);
                authToAdd.Id = id;

                if (!Repo.Save())
                {
                    throw new Exception($"failed to upserting the author {id}");
                }
                var Authtr = Mapper.Map <AuthorToReturn>(authToAdd);
                return(CreatedAtRoute("Author", new { id = Authtr.Id }, Authtr));
            }
            var AuthToPatch = Mapper.Map <Author, AuthorUpdate>(authore);

            auth.ApplyTo(AuthToPatch, ModelState);
            TryValidateModel(AuthToPatch);
            if (AuthToPatch.FirstName == AuthToPatch.LastName)
            {
                ModelState.AddModelError(nameof(AuthorUpdate),
                                         "the firstname and lastname cannot be the same");
            }
            if (!ModelState.IsValid)
            {
                return(new UnProccessableObjectResult(ModelState));
            }

            Mapper.Map(AuthToPatch, authore);

            if (!Repo.Save())
            {
                throw new Exception($"Failed to edit this autor {id}");
            }
            log.LogInformation(204, $"the author with the {authore.Id} has been patch");

            return(NoContent());
        }
        public bool UpdateAuthor(AuthorUpdate model)
        {
            using (var ctx = new ApplicationDbContext())
            {
                var entity = ctx.Authors.Single(e => e.AuthorId == model.AuthorId);

                entity.AuthorName = model.AuthorName;
                entity.Birthdate  = model.Birthdate;

                return(ctx.SaveChanges() == 1);
            }
        }
        public IHttpActionResult Put(AuthorUpdate model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            if (!_service.UpdateAuthor(model))
            {
                return(InternalServerError());
            }

            return(Ok("Author was updated successfully!"));
        }
Beispiel #5
0
 public void Update(long id, AuthorUpdate authorUpdate)
 {
     this.authorApplication.UpdateFromAPI(id, authorUpdate);
 }
Beispiel #6
0
        public IActionResult UpdateAuthor(Guid Id, [FromBody] AuthorUpdate authUpda)
        {
            var auth = Repo.GetAuthor(Id, true);

            //upserting with put
            if (auth == null)
            {
                try
                {
                    if (authUpda.FirstName.ToLower() == authUpda.LastName.ToLower())
                    {
                        ModelState.AddModelError(nameof(AuthorUpdate),
                                                 "the firstname and lastname cannot be the same");
                    }
                }
                catch (Exception)
                {
                    if (authUpda.FirstName == authUpda.LastName)
                    {
                        ModelState.AddModelError(nameof(AuthorUpdate),
                                                 "the firstname and lastname cannot be the same");
                    }
                }


                if (!ModelState.IsValid)
                {
                    return(new UnProccessableObjectResult(ModelState));
                }

                var autor = Mapper.Map <AuthorUpdate, Author>(authUpda);
                Repo.AddAuthor(autor);
                autor.Id = Id;

                if (!Repo.Save())
                {
                    throw new Exception($"failed to create this autor with the {Id}");
                }
                var authorcreated = Mapper.Map <Author, AuthorToReturn>(autor);
                return(CreatedAtRoute("Author", new { id = autor.Id }, autor));
            }

            if (authUpda.FirstName == auth.LastName)
            {
                ModelState.AddModelError(nameof(AuthorUpdate),
                                         "the firstname and lastname cannot be the same");
            }
            if (authUpda.Genre != auth.Genre)
            {
                ModelState.AddModelError(nameof(AuthorUpdate),
                                         "the genre cannot be change");
            }
            if (!ModelState.IsValid)
            {
                return(new UnProccessableObjectResult(ModelState));
            }
            Mapper.Map(authUpda, auth);

            if (!Repo.Save())
            {
                throw new Exception($"Failed to Update autor with the id: {Id}");
            }
            return(NoContent());
        }