Example #1
0
        public ActionResult <CommonResponeModel> SaveChange([FromForm] EmployeeSaveChangeRequestModel model)
        {
            var databaseObject = new Employee();
            int result;

            if (model.Id == 0)
            {
                databaseObject.MapFrom(model);

                if (string.IsNullOrEmpty(databaseObject.Code))
                {
                    var code = entityCenterRepository.GetCodeByEntity(nameof(Employee));

                    if (string.IsNullOrEmpty(code))
                    {
                        Result = new ErrorResult(ActionType.Insert, AppGlobal.MakeCodeError);
                        return(GetCommonRespone());
                    }

                    databaseObject.Code = code;
                }

                if (employeeRepository.IsExistCode(databaseObject.Code))
                {
                    Result = new ErrorResult(ActionType.Insert, CommonMessageGlobal.Exist("Mã nhân viên"));
                    return(GetCommonRespone());
                }

                databaseObject.InitBeforeSave(RequestUsername, InitType.Create);
                databaseObject.InitDefault();

                //lưu hình nếu tồn tại
                if (model.ImageFile != null && string.IsNullOrWhiteSpace(model.ImageFile.FileName))
                {
                    model.ImageFile.SaveTo(AppGlobal.AvatarFolder);
                    databaseObject.Image = Path.Combine(AppGlobal.AvatarFolder, model.ImageFile.FileName);
                }

                result = employeeRepository.Insert(databaseObject);
            }
            else
            {
                databaseObject = employeeRepository.GetById(model.Id);
                databaseObject.MapFrom(model);
                databaseObject.InitBeforeSave(RequestUsername, InitType.Update);
                databaseObject.InitDefault();

                //lưu hình nếu tồn tại
                if (model.ImageFile != null && !string.IsNullOrWhiteSpace(model.ImageFile.FileName))
                {
                    var pathFolder = $@"\Storages\Avatars";
                    var folder     = this._env.WebRootPath + pathFolder;
                    if (!Directory.Exists(folder))
                    {
                        Directory.CreateDirectory(folder);
                    }

                    string filePath = Path.Combine(folder, model.ImageFile.FileName);
                    using (FileStream fs = System.IO.File.Create(filePath))
                    {
                        model.ImageFile.CopyTo(fs);
                        fs.Flush();
                    }

                    databaseObject.Image = Path.Combine(pathFolder, model.ImageFile.FileName).Replace(@"\", @"/");
                }

                result = employeeRepository.Update(databaseObject);
            }

            if (result > 0)
            {
                Result = new SuccessResult(ActionType.Insert, AppGlobal.SaveChangeSuccess);
            }
            else
            {
                Result = new ErrorResult(ActionType.Insert, AppGlobal.SaveChangeFalse);
            }

            return(GetCommonRespone());
        }