Exemplo n.º 1
0
 public ActionResult <Slider> Create([FromBody] Slider slider)
 {
     _sliderService.Create(slider);
     return(Ok());
 }
Exemplo n.º 2
0
        public async Task <ActionResult> AddOrUpdate(SliderViewModel model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(Json(new AjaxResult(ModelState.JoinMessages())));
                }

                var isUpdate = model.Id > 0;

                model.Pictures?.ForEach(x => x.Link = FileHelper.CopyFile(x.Link, AppConst.Folder.Slider));
                if (isUpdate)
                {
                    var pics = _service.SliderRepository.Asset(model.Id).SelectMany(x => x.Pictures).ToList();
                    pics.ForEach(x =>
                    {
                        if (!model.Pictures.Select(a => a.Link).Contains(x.Link))
                        {
                            FileHelper.RemoveFile(x.Link);
                        }
                    });
                }

                var result = isUpdate ? await _service.Update(model.Id, model) : await _service.Create(model);

                if (result.NotFound)
                {
                    return(Json(new AjaxResult("اسلایدر مورد نظر یافت نشد.")));
                }

                if (result.Succeeded)
                {
                    model.Pictures?.ForEach(x =>
                    {
                        if (!string.Equals(x.Link, x.LinkOld, StringComparison.CurrentCultureIgnoreCase))
                        {
                            FileHelper.RemoveFile(x.LinkOld);
                        }
                    });
                    return(Json(new AjaxResult(true, "اطلاعات با موفقیت ذخیره گردید.")));
                }

                return(Json(new AjaxResult(result.State.Errors.JoinMessages())));
            }
            catch (Exception e)
            {
                return(Json(new AjaxResult(e.JoinMessages())));
            }
        }