public void TestUpdateWebsiteAsync_WithValidParameters_ShouldNotThrowAnyException() { var websiteId = 1; var websiteModel = new UpdateWebsiteModel() { CompanyId = 1, Name = "Website Update", UrlPath = "UrlPath Update", Note = "Note Update" }; Assert.DoesNotThrowAsync(() => websiteService.UpdateAsync(websiteId, websiteModel)); }
public void TestUpdateWebsiteAsync_WithCompanyAlreadyHasWebsite_ShouldThrowValidationException() { var websiteId = 1; var websiteModel = new UpdateWebsiteModel() { CompanyId = 2, Name = "Website Update", UrlPath = "UrlPath Update", Note = "Note Update" }; var ex = Assert.ThrowsAsync <ValidationException>(() => websiteService.UpdateAsync(websiteId, websiteModel)); Assert.AreEqual(Constants.MessageResponse.UniqueWebsitePerCompanyError, ex.Message); }
public void TestUpdateWebsiteAsync_WithNotExistedCompany_ShouldThrowNotFoundException() { var websiteId = 1; var websiteModel = new UpdateWebsiteModel() { CompanyId = 10, Name = "Website Update", UrlPath = "UrlPath Update", Note = "Note Update" }; var ex = Assert.ThrowsAsync <NotFoundException>(() => websiteService.UpdateAsync(websiteId, websiteModel)); Assert.AreEqual(string.Format(Constants.MessageResponse.NotFoundError, nameof(Company), websiteModel.CompanyId.ToString()), ex.Message); }
public void TestUpdateWebsiteyAsync_WithUrlPathOfWebsiteExisted_ShouldThrowValidationException() { var websiteId = 1; var websiteModel = new UpdateWebsiteModel() { CompanyId = 1, Name = "Website Update", UrlPath = "cmc", Note = "Note Update" }; var ex = Assert.ThrowsAsync <ValidationException>(() => websiteService.UpdateAsync(websiteId, websiteModel)); Assert.AreEqual(Constants.MessageResponse.WebsiteUrlPathExisted, ex.Message); }
/// <summary> /// Update existed website /// </summary> /// <param name="websiteId"></param> /// <param name="websiteModel"></param> /// <returns></returns> public async Task UpdateAsync(int websiteId, UpdateWebsiteModel websiteModel) { var websiteRepository = unitOfWork.GetRepository <Website>(); var companyRepository = unitOfWork.GetRepository <Company>(); var oldPath = ""; if (websiteModel.IsNewImage) { oldPath = Path.Combine(Environment.CurrentDirectory, websiteModel.LogoUrl); var logoUrl = $"Assets\\Images\\{websiteModel.UrlPath}_{DateTime.Now:yyMMddHHmmss}{Path.GetExtension(oldPath)}"; string newPath = Path.Combine(Environment.CurrentDirectory, logoUrl); File.Move(oldPath, newPath); websiteModel.LogoUrl = logoUrl; } var website = await websiteRepository.GetAsync(websiteId); if (website == null) { if (websiteModel.IsNewImage) { File.Delete(oldPath); } throw new NotFoundException( string.Format(Constants.MessageResponse.NotFoundError, nameof(Website), websiteId.ToString())); } if (website.Name.ToLower() != websiteModel.Name.ToLower()) { var isExistedName = await websiteRepository.ExistsAsync(c => c.Name.Equals(websiteModel.Name)); if (isExistedName) { if (websiteModel.IsNewImage) { File.Delete(oldPath); } throw new ValidationException(Constants.MessageResponse.WebsiteNameExisted); } } if (website.UrlPath != websiteModel.UrlPath) { var isExistedUrlPath = await websiteRepository.ExistsAsync(c => c.UrlPath.Equals(websiteModel.UrlPath)); if (isExistedUrlPath) { if (websiteModel.IsNewImage) { File.Delete(oldPath); } throw new ValidationException(Constants.MessageResponse.WebsiteUrlPathExisted); } } var company = await companyRepository.GetAsync(websiteModel.CompanyId); if (company == null) { if (websiteModel.IsNewImage) { File.Delete(oldPath); } throw new NotFoundException(string.Format(Constants.MessageResponse.NotFoundError, nameof(Company), websiteModel.CompanyId.ToString())); } if (company != null && websiteModel.CompanyId != website.CompanyId) { if (websiteModel.IsNewImage) { File.Delete(oldPath); } throw new ValidationException(Constants.MessageResponse.UniqueWebsitePerCompanyError); } mapper.Map(websiteModel, website); website.UpdatedDate = DateTime.UtcNow; websiteRepository.Update(website); await unitOfWork.CommitAsync(); }
public async Task <IActionResult> Put(int id, [FromBody] UpdateWebsiteModel websiteModel) { await websiteService.UpdateAsync(id, websiteModel); return(Ok(new ApiResponse <UpdateWebsiteModel>())); }