private static void SeedCategories(ApplicationDbContext context) { if (context.WebsiteCategories.Count() > 0) { return; } var testCategory1 = new WebsiteCategory { Name = "Sports", DateCreated = UtilityConstants.Now, DateModified = UtilityConstants.Now }; var testCategory2 = new WebsiteCategory { Name = "Leasure", DateCreated = UtilityConstants.Now, DateModified = UtilityConstants.Now }; context.WebsiteCategories.Add(testCategory1); context.WebsiteCategories.Add(testCategory2); context.SaveChanges(); }
/// <summary> /// The convert database categories to website categories. /// </summary> /// <param name="databaseCategories"> /// The database categories. /// </param> /// <returns> /// The <see cref="List"/>. /// </returns> public List <WebsiteCategory> ConvertDatabaseCategoriesToWebsiteCategories(List <ProviderServiceCategory> databaseCategories) { if (databaseCategories == null) { return(null); } var list = new List <WebsiteCategory>(); foreach (var databaseCategory in databaseCategories) { var newCategory = new WebsiteCategory { Id = databaseCategory.ID, Name = databaseCategory.Name, Description = databaseCategory.Description, Crime = databaseCategory.Crime, ServiceTypes = new List <WebsiteCategoryType>() }; foreach (var databaseType in databaseCategory.CategoryTypes) { newCategory.ServiceTypes.Add( new WebsiteCategoryType { Id = databaseType.ServiceType.ID, Name = databaseType.ServiceType.Name }); } list.Add(newCategory); } return(list); }
public void GetDetailsTest() { var sampleCategory = new WebsiteCategory { Crime = false, Description = "description", Id = 1, Name = "Name", ServiceTypes = new List <WebsiteCategoryType> { new WebsiteCategoryType { Id = 2, Name = "Category1" } } }; var sampleServiceProvider = new WebsiteServiceProvider { Description = "Service Provider", DisplayRank = 3, Id = 2, IsActive = true, Name = "My Name", Type = 1, Services = new WebServiceAreas { ServiceAreas = new List <int> { 1, 3 } }, Locations = new List <ServiceProviderLocation> { new ServiceProviderLocation { Id = 1, Display = true, Name = "Location Name", City = "Dayton", Zip = "12344" } } }; var categoryList = new List <WebsiteCategory> { sampleCategory }; var controller = new SpecialPopulationsController(); using (ShimsContext.Create()) { ShimDataLogics.AllInstances.GetWebsiteCategories = logics => categoryList; ShimDatabaseToWebServiceProvider.AllInstances.GetServiceProviderInt32 = (provider, i) => sampleServiceProvider; var result = controller.Details(2, null) as ViewResult; var resultModel = result.Model; Assert.IsNotNull(result); Assert.AreEqual(sampleServiceProvider, resultModel); } }
public async Task <IActionResult> Put( [Required] int id, [FromForm] BindWebsite updatedWebsite, [FromForm] [MaxFileSizeInMegabytes(2)] [OnlyJpegAndPngAllowed] IFormFile homepageSnapshot) { Website website = await _context .Websites .Include(w => w.Category) .Include(w => w.HomepageSnapshot) .Include(w => w.Login) .FirstOrDefaultAsync(w => w.Id == id && w.IsDeleted == false); website.Name = updatedWebsite.Name; website.URL = updatedWebsite.URL; website.Name = updatedWebsite.Name; if (website.Category.Id != updatedWebsite.CategoryId) { WebsiteCategory category = await _context.Categories.FirstOrDefaultAsync(cat => cat.Id == updatedWebsite.CategoryId); if (category == null) { throw new ArgumentOutOfRangeException("CategoryId"); } website.Category = category; } if (website.Login.Email != updatedWebsite.LoginEmail || _passwordSafe.Decrypt(website.Login.Password) != updatedWebsite.LoginPassword) { website.Login = new WebsiteCredentials() { Email = updatedWebsite.LoginEmail, Password = _passwordSafe.Encrypt(updatedWebsite.LoginPassword) }; } if (homepageSnapshot != null) { HomepageSnapshot snapshot = await _saveSnapshot.SaveAsync(homepageSnapshot); website.HomepageSnapshot = snapshot; } _context.Update(website); await _context.SaveChangesAsync(); return(NoContent()); }
public async Task <IActionResult> Post( [FromForm] BindWebsite website, [Required] [FromForm] [MaxFileSizeInMegabytes(2)] [OnlyJpegAndPngAllowed] IFormFile homepageSnapshot) { try { HomepageSnapshot newHomepageSnapshot = await _saveSnapshot.SaveAsync(homepageSnapshot); WebsiteCategory category = await _context.Categories.FirstOrDefaultAsync(cat => cat.Id == website.CategoryId); string encryptedPassword = _passwordSafe.Encrypt(website.LoginPassword); var newWebsite = new Website() { Name = website.Name, URL = website.URL, Category = category, HomepageSnapshot = newHomepageSnapshot, Login = new WebsiteCredentials() { Email = website.LoginEmail, Password = encryptedPassword } }; _context.Websites.Add(newWebsite); await _context.SaveChangesAsync(); return(Created( UriHelper.BuildAbsolute( Request.Scheme, Request.Host, Request.PathBase, Request.Path.Add("/" + newWebsite.Id.ToString())), OutputWebsite.FromWebsiteEntityWithFullImageUrl( newWebsite, Request.Scheme, Request.Host, _passwordSafe.Decrypt(newWebsite.Login.Password)))); }catch (Exception ex) { //ToDo: log exception throw ex; } }