Пример #1
0
        public HttpResponseMessage Update(HttpRequestMessage request, SlideViewModel slideVm)
        {
            return(CreateHttpResponse(request, () =>
            {
                HttpResponseMessage response = null;
                if (!ModelState.IsValid)
                {
                    response = request.CreateResponse(HttpStatusCode.BadRequest, ModelState);
                }
                else
                {
                    var dbSlide = _slideService.FindById(slideVm.ID);

                    dbSlide.UpdateSlide(slideVm);
                    dbSlide.UpdatedDate = DateTime.Now;
                    dbSlide.UpdatedBy = User.Identity.Name;
                    _slideService.Update(dbSlide);
                    _slideService.SaveChanges();

                    var responseData = Mapper.Map <Slide, SlideViewModel>(dbSlide);
                    response = request.CreateResponse(HttpStatusCode.Created, responseData);
                }

                return response;
            }));
        }
Пример #2
0
        public HttpResponseMessage Update(HttpRequestMessage request, SlideViewModel slideViewModel)
        {
            return(CreateHttpResponse(request, () =>
            {
                HttpResponseMessage response = null;
                if (!ModelState.IsValid)
                {
                    //trả về lỗi để bên ngoài bắt được sự kiện lỗi này
                    response = request.CreateResponse(HttpStatusCode.BadRequest, ModelState);
                }
                else
                {
                    var dbSlide = _sildeService.GetById(slideViewModel.ID);
                    dbSlide.UpdateSlide(slideViewModel);

                    _sildeService.Update(dbSlide);
                    _sildeService.Save();

                    var responseData = Mapper.Map <Slide, SlideViewModel>(dbSlide);
                    response = request.CreateResponse(HttpStatusCode.Created, responseData);
                }

                return response;
            }));
        }
Пример #3
0
        public ActionResult Put(SlideViewModel slideVm)
        {
            var message = string.Empty;

            if (ModelState.IsValid)
            {
                var slide = _SlideService.GetById(slideVm.ID);
                if (slide != null)
                {
                    try
                    {
                        slide.UpdateSlide(slideVm);
                        _SlideService.Update(slide);
                        _SlideService.Save();
                        message = ResultState.Update_SUCCESS;
                        return(RedirectToAction("Index", "Slide"));
                    }
                    catch (Exception e)
                    {
                        ModelState.AddModelError("", e.Message);
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Không tìm dữ liệu nào!");
                }
            }
            else
            {
                ModelState.AddModelError("", ResultState.Update_FALSE);
            }

            return(View(slideVm));
        }
Пример #4
0
 public async Task <IActionResult> SaveEntity(SlideViewModel slideViewModel)
 {
     if (!ModelState.IsValid)
     {
         var allErrors = ModelState.Values.SelectMany(v => v.Errors);
         return(new BadRequestObjectResult(allErrors));
     }
     else
     {
         if (slideViewModel.Id == 0)
         {
             var notificationId = Guid.NewGuid().ToString();
             var announcement   = new AnnouncementViewModel
             {
                 Title       = User.GetSpecificClaim("FullName"),
                 DateCreated = DateTime.Now,
                 Content     = $"Slide {slideViewModel.Name} has been created",
                 Id          = notificationId,
                 UserId      = User.GetUserId(),
                 Image       = User.GetSpecificClaim("Avatar"),
                 Status      = Status.Active
             };
             var announcementUsers = new List <AnnouncementUserViewModel>()
             {
                 new AnnouncementUserViewModel()
                 {
                     AnnouncementId = notificationId, HasRead = false, UserId = User.GetUserId()
                 }
             };
             _slideService.Add(announcement, announcementUsers, slideViewModel);
             await _hubContext.Clients.All.SendAsync("ReceiveMessage", announcement);
         }
         else
         {
             var notificationId = Guid.NewGuid().ToString();
             var announcement   = new AnnouncementViewModel
             {
                 Title       = User.GetSpecificClaim("FullName"),
                 DateCreated = DateTime.Now,
                 Content     = $"Slide {slideViewModel.Name} has been updated",
                 Id          = notificationId,
                 UserId      = User.GetUserId(),
                 Image       = User.GetSpecificClaim("Avatar"),
                 Status      = Status.Active
             };
             var announcementUsers = new List <AnnouncementUserViewModel>()
             {
                 new AnnouncementUserViewModel()
                 {
                     AnnouncementId = notificationId, HasRead = false, UserId = User.GetUserId()
                 }
             };
             _slideService.Update(announcement, announcementUsers, slideViewModel);
             await _hubContext.Clients.All.SendAsync("ReceiveMessage", announcement);
         }
         _slideService.Save();
         return(new OkObjectResult(slideViewModel));
     }
 }
Пример #5
0
        public async Task <IActionResult> Update([FromForm] SlideUpdateRequest request)
        {
            var result = await _slideService.Update(request);

            if (!result.IsSuccess)
            {
                return(BadRequest(result));
            }
            return(Ok(result));
        }
Пример #6
0
 public IActionResult SaveEntity(SlideViewModel slideVm)
 {
     if (!ModelState.IsValid)
     {
         IEnumerable <ModelError> allErrors = ModelState.Values.SelectMany(v => v.Errors);
         return(new BadRequestObjectResult(allErrors));
     }
     if (slideVm.Id == 0)
     {
         _slideService.Add(slideVm);
     }
     else
     {
         _slideService.Update(slideVm);
     }
     _slideService.SaveChanges();
     return(new OkObjectResult(slideVm));
 }
Пример #7
0
 public HttpResponseMessage Update(HttpRequestMessage request, SlideViewModel slideVm)
 {
     if (ModelState.IsValid)
     {
         Slide slideDb = _slideService.GetById(slideVm.ID);
         if (slideDb.Image != slideVm.Image && slideDb.Image != null)
         {
             DeleteElementImage(slideDb.Image);
         }
         slideDb.UpdateSlide(slideVm);
         _slideService.Update(slideDb);
         _slideService.SaveChange();
         return(request.CreateResponse(HttpStatusCode.Created, slideVm));
     }
     else
     {
         return(request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
     }
 }
 public IActionResult SaveEntity(SlideViewModel slideViewModel)
 {
     if (!ModelState.IsValid)
     {
         var allErrors = ModelState.Values.SelectMany(v => v.Errors);
         return(new BadRequestObjectResult(allErrors));
     }
     else
     {
         if (slideViewModel.Id == 0)
         {
             _slideService.Add(slideViewModel);
         }
         else
         {
             _slideService.Update(slideViewModel);
         }
         _slideService.Save();
         return(new OkObjectResult(slideViewModel));
     }
 }
Пример #9
0
        public HttpResponseMessage Put(HttpRequestMessage request, SlideViewModel slideVm)
        {
            return(CreateHttpResponse(request, () =>
            {
                HttpResponseMessage response = null;
                if (!ModelState.IsValid)
                {
                    request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
                }
                else
                {
                    var slideDb = _slideService.GetByID(slideVm.ID);
                    slideDb.UpdateSlide(slideVm);
                    _slideService.Update(slideDb);
                    _slideService.SaveChanges();

                    response = request.CreateResponse(HttpStatusCode.OK);
                }
                return response;
            }));
        }
Пример #10
0
        public async Task <IActionResult> Update([FromBody] SlideViewModel slideVm)
        {
            var hasPermission = await _authorizationService.AuthorizeAsync(User, "SLIDE", Operations.Update);

            if (hasPermission.Succeeded == false)
            {
                return(new BadRequestObjectResult(CommonConstants.Forbidden));
            }
            if (ModelState.IsValid)
            {
                Slide  slideDb = _slideService.GetByIdDb(slideVm.Id);
                string oldPath = slideDb.Image;
                if (oldPath != slideVm.Image && !string.IsNullOrEmpty(oldPath))
                {
                    oldPath.DeletementByString(_env);
                }
                slideDb.UpdateSlide(slideVm);
                _slideService.Update(slideDb);
                _slideService.SaveChanges();
                return(new OkObjectResult(slideVm));
            }
            return(new BadRequestObjectResult(ModelState));
        }
Пример #11
0
        public IActionResult SaveEntity(SlideViewModel slideVm, int position)
        {
            try
            {
                if (slideVm.Id == 0)
                {
                    var errorBySlideName = "Tên slide đã tồn tại";
                    if (_slideService.ValidateAddSlideName(slideVm))
                    {
                        ModelState.AddModelError("",
                                                 errorBySlideName);
                    }

                    var errorBySortOrder = "Thứ tự đã tồn tại";
                    if (_slideService.ValidateAddSortOrder(slideVm, position))
                    {
                        ModelState.AddModelError("",
                                                 errorBySortOrder);
                    }

                    if (!ModelState.IsValid)
                    {
                        return(BadRequest(ModelState.Select(x => x.Value.Errors).FirstOrDefault(y => y.Count > 0)?.First()
                                          .ErrorMessage));
                    }
                    _slideService.Add(slideVm);
                }
                else
                {
                    var errorBySlideName = "Tên slide đã tồn tại";
                    if (_slideService.ValidateUpdateSlideName(slideVm))
                    {
                        ModelState.AddModelError("",
                                                 errorBySlideName);
                    }

                    var errorBySortOrder = "Thứ tự đã tồn tại";
                    if (_slideService.ValidateUpdateSortOrder(slideVm, position))
                    {
                        ModelState.AddModelError("",
                                                 errorBySortOrder);
                    }

                    if (!ModelState.IsValid)
                    {
                        return(BadRequest(ModelState.Select(x => x.Value.Errors).FirstOrDefault(y => y.Count > 0)?.First()
                                          .ErrorMessage));
                    }

                    _slideService.Update(slideVm);
                }

                //if (!ModelState.IsValid)
                //{
                //    IEnumerable<ModelError> allErrors = ModelState.Values.SelectMany(v => v.Errors);
                //    return new BadRequestObjectResult(allErrors);
                //}

                _slideService.Save();
                return(new OkObjectResult(slideVm));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Пример #12
0
        public IActionResult Put([FromBody] Slide body)
        {
            var entity = _manager.Update(body);

            return(ResponseJson(_manager.Update(entity)));
        }