Exemplo n.º 1
0
        public ActionResult EditArtice(Guid? id)
        {
            var model = new ArticeModel();

            if (id.HasValue)
            {
                var entity = ServiceHelper.Artice.ExecuteDispose(s => s.GetArtice(id.Value));

                if (entity == null)
                {
                    return JsonObject(false, BackendMessage.CannotLoadData);
                }

                model = entity.Map<Artice, ArticeModel>();
                model.OldImagePath = model.ImagePath;
            }

            return JsonObject(true, string.Empty, new
            {
                html = PartialViewToString("_edit", model),
                imagePath = Url.ImageIsExist(model.ImagePath) ? Url.ImageLink(model.ImagePath) : string.Empty
            });
        }
Exemplo n.º 2
0
        public ActionResult SaveArtice(ArticeModel model)
        {
            if (model.IsNew && !CanAdd)
            {
                return GetAddDeniedResult();
            }

            if (!model.IsNew && !CanUpdate)
            {
                return GetUpdateDeniedResult();
            }

            if (!ModelState.IsValid)
            {
                return JsonObject(false, GetModelStateErrors());
            }

            #region save image

            if (Request.Files["fileImage"] != null && !string.IsNullOrWhiteSpace(Request.Files["fileImage"].FileName))
            {
                var file = Request.Files["fileImage"];

                var extension = Path.GetExtension(file.FileName).ToStr();

                if (!SiteUtils.IsImageFile(file.FileName))
                {
                    return JsonObject(false, BackendMessage.FileTypeIsInvalid);
                }

                if (!SiteUtils.ImageSizeIsValid(file.ContentLength))
                {
                    return JsonObject(false, BackendMessage.FileMaxSize5MB);
                }

                if (!string.IsNullOrWhiteSpace(model.OldImagePath))
                {
                    DeleteImageFile(model.OldImagePath);
                }

                model.ImagePath = Guid.NewGuid() + extension;

                var filePath = PhysicalDataFilePath(model.ImagePath);
                file.SaveAs(filePath);
            }
            else if (string.IsNullOrWhiteSpace(model.ImagePath) && !string.IsNullOrWhiteSpace(model.OldImagePath))
            {
                DeleteImageFile(model.OldImagePath);
            }

            #endregion

            var entity = model.Map<ArticeModel, Artice>();

            if (entity.IsNew)
            {
                entity.InitId();
                entity.CreatedDate = DateTime.UtcNow;
            }
            else
            {
                var oldData = ServiceHelper.Artice.ExecuteDispose(s => s.GetArtice(entity.Id));

                if (oldData == null)
                {
                    return JsonObject(false, BackendMessage.CannotLoadData);
                }

                entity.CreatedDate = oldData.CreatedDate;
            }

            var request = new SaveRequest
            {
                Entity = entity
            };

            var res = ServiceHelper.Artice.ExecuteDispose(s => s.SaveArtice(request));

            if (res.Success)
            {
                SendNotification(NotifyType.Success, BackendMessage.SaveDataSuccess);
                return JsonObject(true, string.Empty);
            }

            var msg = res.Messages.FirstOrDefault();

            return JsonObject(false, string.IsNullOrWhiteSpace(msg) ? BackendMessage.ErrorOccure : msg.GetServiceMessageRes());
        }