public async Task UpdateNationality([FromForm] NationalityRequest input) { bool checkName = await WorkScope.GetAll <Nationality>().AnyAsync(x => x.Name == input.Name && x.Id != input.Id); if (checkName) { throw new UserFriendlyException("Quốc tịch đã tồn tại"); } var nationality = await WorkScope.GetAll <Nationality>().FirstOrDefaultAsync(x => x.Id == input.Id); if (nationality == default) { throw new UserFriendlyException("Quốc tịch không tồn tại"); } nationality.Name = input.Name; if (input.Image?.Length > 0) { string fileLocation = UploadFiles.CreateFolderIfNotExists(ConstantVariable.RootFolder, $@"{ConstantVariable.UploadFolder}\{ConstantVariable.Company}"); string fileName = await UploadFiles.UploadAsync(fileLocation, input.Image); nationality.Path = $"{ConstantVariable.UploadFolder}/{ConstantVariable.Company}/{fileName}"; } await WorkScope.UpdateAsync(nationality); }
public async Task CreateNationalityAsync([FromForm] NationalityRequest input) { bool checkName = await WorkScope.GetAll <Nationality>().AnyAsync(x => x.Name == input.Name); if (checkName) { throw new UserFriendlyException("Quốc tịch đã tồn tại"); } string fileName = string.Empty; string fileLocation = UploadFiles.CreateFolderIfNotExists(ConstantVariable.RootFolder, $@"{ConstantVariable.UploadFolder}\{ConstantVariable.Nationality}"); if (input.Image?.Length > 0) { fileName = await UploadFiles.UploadAsync(fileLocation, input.Image); } var nationality = new Nationality { Name = input.Name, Path = $"{ConstantVariable.UploadFolder}/{ConstantVariable.Nationality}/{fileName}" }; await WorkScope.InsertAsync(nationality); }