/// <summary> /// Commits an upload. /// </summary> /// <param name="tenantId">Tenant identifier.</param> /// <param name="uploadId">Upload identifier.</param> /// <param name="storageHierarchy">New location of upload, once committed. E.g. { "Uploads" > "Users" }.</param> /// <param name="unitOfWork">Unit of work.</param> public void Commit(long tenantId, long uploadId, List <string> storageHierarchy, IUnitOfWork unitOfWork = null) { // If we don't have a unit of work in place, create one now so that we can rollback all changes in case of failure IUnitOfWork localUnitOfWork = unitOfWork == null?_unitOfWorkFactory.CreateUnitOfWork() : null; // Try to commit upload try { // Get location of uncommitted uploads List <string> uncommittedStorageHierarchy = GetUncommittedStorageHierarchy(); // Get upload Upload upload = Read(tenantId, uploadId, uncommittedStorageHierarchy, unitOfWork ?? localUnitOfWork); // Set committed true upload.Committed = true; upload.Updated = DateTime.UtcNow; // Update upload so that committed is true switch (upload.UploadType) { case UploadType.Upload: _uploadRepository.UpdateUpload(upload, unitOfWork ?? localUnitOfWork); break; case UploadType.Image: _uploadRepository.UpdateImage((Image)upload, unitOfWork ?? localUnitOfWork); break; } // Create copy of upload with new storage hierarchy _storageService.Create(upload, storageHierarchy, unitOfWork ?? localUnitOfWork); // Delete uncommitted upload from storage _storageService.Delete(upload, uncommittedStorageHierarchy, unitOfWork ?? localUnitOfWork); // Commit work if local unit of work in place, then return newly allocated upload identifier if (localUnitOfWork != null) { localUnitOfWork.Commit(); } } catch { if (localUnitOfWork != null) { localUnitOfWork.Rollback(); } throw; } finally { if (localUnitOfWork != null) { localUnitOfWork.Dispose(); } } }
/// <summary> /// Update person /// </summary> /// <param name="person"></param> /// <param name="objectFile"></param> /// <returns></returns> public async Task <PersonViewModel> UpdatePerson(string file, Person person, FileUpload objectFile) { var personModel = await _personRepository.FindAsync(person.Id); if (personModel == null) { model.AppResult.Message = Constant.PERSONID_ERROR; return(model); } //check existing person by id person var countPerson = _personRepository.CheckPersonExisting(person.Id); if (countPerson.Result == 0) { model.AppResult.Message = Constant.PERSONID_ERROR; return(model); } model.PersonInfo = person; model.PersonInfo.UpdatedBy = WebAPI.Helpers.HttpContext.CurrentUser; var genderValue = Convert.ToInt32(model.PersonInfo.Gender); if (person.FullName == "" || person.FullName == null || person.FullName.GetType() != typeof(string) || person.FullName.Length > 256) { model.AppResult.Message = Constant.NAME_ERROR; return(model); } else if (Functions.CheckLocation(Convert.ToInt32(model.PersonInfo.Location)) != true) { model.AppResult.Message = Constant.LOCATION_INVALID; return(model); } else if (Functions.IsPhoneNumber(person.Phone) == false || person.Phone == null || await _personRepository.CheckPhoneToUpdate(person.Id, person.Phone) > 0) { model.AppResult.Message = Constant.PHONE_ERROR; return(model); } else if (genderValue < 0 || genderValue > 2) { model.AppResult.Message = Constant.GENDER_INVALID; return(model); } #region Validate phone /* else if (Functions.IsPhoneNumber(person.Phone) == false) * { * model.AppResult = new AppResult { Result = false, StatusCd = "400", Message = "Phone invalid.!", DataResult = null }; * return model; * }*/ #endregion else if (Functions.HandlingEmail(person.Email) != true || await _personRepository.CheckEmailUpdate(person.Id, person.Email) > 0) { model.AppResult.Message = Constant.EMAIL_ERROR; return(model); } else if (!Functions.ValidateYearOfBirth(person.YearOfBirth)) { model.AppResult.Message = Constant.YEAROFBIRTH_INVALID; return(model); } else { //get \\Avatar\\ from appseting.json var ImagePath = _appSettings.ImagePath.ToString(); //return domains var host = WebAPI.Helpers.HttpContext.Current.Request.Host.Value; string path = _webHostEnvironment.WebRootPath + ImagePath; //Reset avatar if (file == "reset" && objectFile.files == null) { string imageName = "avatar-default.png"; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } await _uploadRepository.UpdateImage(imageName, person.Id); await _personRepository.UpdateAsync(model.PersonInfo); model.PersonInfo.Description = await _personRepository.GetDescription(person.Id); model.PersonInfo.Avatar = imageName; model.AppResult.Message = Constant.UPDATE_PERSON_SUCCESS; model.AppResult.DataResult = model.PersonInfo; return(model); } //Update person not choose avatar if (objectFile.files == null) { string imageName = personModel.Avatar; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } await _uploadRepository.UpdateImage(imageName, person.Id); await _personRepository.UpdateAsync(model.PersonInfo); model.PersonInfo.Description = await _personRepository.GetDescription(person.Id); model.PersonInfo.Avatar = imageName; model.AppResult.Message = Constant.UPDATE_PERSON_SUCCESS; model.AppResult.DataResult = model.PersonInfo; return(model); } //Update person and choose avatar if (Functions.HasImageExtension(Path.GetExtension(objectFile.files.FileName.ToLower())) == false) { model.AppResult.Message = Constant.IMAGE_INVALID; return(model); } else { if (objectFile.files.Length > 0) { if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } var imageName = await _personRepository.GetStaffIdPerson(person.Id); string fileName = imageName + Path.GetExtension(objectFile.files.FileName.ToLower()); using (FileStream fileStream = System.IO.File.Create(path + fileName)) { objectFile.files.CopyTo(fileStream); fileStream.Flush(); await _uploadRepository.UpdateImage(fileName, person.Id); //return http request and infor person created await _personRepository.UpdateAsync(model.PersonInfo); model.PersonInfo.Description = await _personRepository.GetDescription(person.Id); model.PersonInfo.Avatar = fileName; model.AppResult.Message = Constant.UPDATE_PERSON_SUCCESS; model.AppResult.DataResult = model.PersonInfo; return(model); } } else { //return http error request model.AppResult.Message = "Create person Failed!"; model.AppResult.DataResult = model.PersonInfo; return(model); } } } }