コード例 #1
0
        public IActionResult Patch(string id, [FromBody] User user)
        {
            if (user == null)
            {
                return(BadRequest());
            }

            if (ModelState.IsValid)
            {
                User userToUpdate = _context.Find <User>(int.Parse(id));

                if (userToUpdate != null)
                {
                    user.UserId = int.Parse(id);

                    // TODO need to figure out how to update just the values offered by the user? or maybe just figure out a way of validating that
                    // TODO on the client side
                    _context.Entry(userToUpdate).CurrentValues.SetValues(user);

                    _context.Update(userToUpdate);

                    _context.SaveChanges();

                    return(CreatedAtRoute("GetById", new { id = userToUpdate.UserId }, userToUpdate));
                }

                return(NotFound());
            }



            return(null);
        }
コード例 #2
0
        public IActionResult PutUser(int id, [FromBody] User user)
        {
            if (id != user.id)
            {
                return(BadRequest());
            }

            _context.Entry(user).State = EntityState.Modified;

            try
            {
                _context.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (_context.User.Find(id) == null)
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
コード例 #3
0
        public async Task <IActionResult> PutUser(int id, User user)
        {
            if (id != user.Id)
            {
                return(BadRequest());
            }

            _context.Entry(user).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!UserExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
コード例 #4
0
ファイル: BlogpostsController.cs プロジェクト: ZijahM/DemoApi
        public ActionResult <PostDTO> PutBlogpost(string slug, PostDTO blogpost)
        {
            var id = _context.Blogposts.Where(x => x.Slug == slug).Select(x => x.Id).FirstOrDefault();

            blogpost.BlogPost.Id        = id;
            blogpost.BlogPost.Slug      = Helper.GenerateSlug(blogpost.BlogPost.Title);
            blogpost.BlogPost.UpdatedAt = DateTime.UtcNow;

            _context.Entry(blogpost.BlogPost).State = EntityState.Modified;

            try
            {
                _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!BlogpostExists(slug))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }
            PostDTO result = new PostDTO();

            result.BlogPost = blogpost.BlogPost;
            return(result);
        }
コード例 #5
0
        public IActionResult Put(User user)
        {
            var db        = new MyWebApiContext(DbContextOptions);
            var foundUser = db.Users.FirstOrDefault(us => us.Id == user.Id);

            if (foundUser != null)
            {
                foundUser.Name            = user.Name;
                foundUser.DepartmentId    = user.DepartmentId;
                foundUser.Position        = user.Position;
                foundUser.Salary          = user.Salary;
                db.Entry(foundUser).State = EntityState.Modified;
            }

            db.SaveChanges();
            return(RedirectPermanent("/"));
        }
コード例 #6
0
        public ActionResult UpdateArticle(int id, Article article)
        {
            //an article cannot change its ID, nor to be assigned with unexisting user, also the status must be one of the already existing
            if (id != article.Id || _context.Users.Find(article.UserID) == null || Article.IsStatusValid(ArticleStatus, article.Status))
            {
                RabbitCommunicator.queueName = "errors";
                RabbitCommunicator.message   = $"Error in article update";
                RabbitCommunicator.PublishMessage(RabbitCommunicator.queueName, RabbitCommunicator.message, _context.RabbitMQChannel);

                return(BadRequest());
            }
            _context.Entry(article).State = EntityState.Modified;
            _context.SaveChanges();

            RabbitCommunicator.queueName = "article";
            RabbitCommunicator.message   = $"An article with id {article.Id} was updated";
            RabbitCommunicator.PublishMessage(RabbitCommunicator.queueName, RabbitCommunicator.message, _context.RabbitMQChannel);

            return(NoContent());
        }