public static async Task AddSeedUsersData(IServiceProvider services) { // Get UserManager Service to Application UserManager <AppUser> userManager = services.GetRequiredService <UserManager <AppUser> >(); var context = services.GetRequiredService <AppDbContext>(); Random random = new Random(); // create Users list for (int i = 1; i <= 100; i++) { var Confirmed = true; var name = "visitor"; var role = "Visitor"; var AuPass = false; var lang = "de,"; if (i >= 20 && i < 25) { AuPass = true; } if (i >= 25 && i < 30) { name = "admin"; role = "Admin"; } if (i >= 30 && i < 35) { name = "editor"; role = "Editor"; } if (i >= 40 && i < 50) { Confirmed = false; } if (i % 2 == 0) { lang = "de,ar,"; } if (i % 7 == 0) { lang = "ar,"; } if (i % 8 == 1) { lang = "en,fr,"; } var userS = new AppUser { Email = name + "_" + i + "@al-alloush.com", EmailConfirmed = Confirmed, UserName = name + "_" + i, FirstName = "First " + name + " Name", LastName = "Last " + name + " Name", SelectedLanguages = lang, RegisterDate = DateTime.Now, Birthday = DateTime.Parse("15/6/1978", MyCultureInfo), PhoneNumber = "17328547" + i, PhoneNumberConfirmed = Confirmed, AutomatedPassword = AuPass, Address = new Address { Street = "street_" + i, BuildingNum = "12", Flore = "L 3", City = "city_" + i, Zipcode = "874" + i, Country = "German" } }; IdentityResult createS = await userManager.CreateAsync(userS, "!QA1qa"); if (createS.Succeeded) { userManager.AddToRoleAsync(userS, role).Wait(); } } var users = await context.Users.ToArrayAsync(); for (int i = 1; i < users.Length; i++) { var imgNum = random.Next(1, 52); var upload = new Upload { Name = "user (" + imgNum + "): " + random.Next(1, 100000), Path = "/Uploads/Images/user (" + imgNum + ").jpg", AddedDateTime = DateTime.Now, UserId = users[i].Id }; await context.Upload.AddAsync(upload); await context.SaveChangesAsync(); } for (int i = 1; i < users.Length; i++) { var userUploads = await context.Upload.FirstOrDefaultAsync(u => u.UserId == users[i].Id); var userImage = new UploadUserImagesList { UploadId = userUploads.Id, UserId = users[i].Id, Default = true, UploadTypeId = 1 }; await context.UploadUserImagesList.AddAsync(userImage); await context.SaveChangesAsync(); } }
// END //********************************************************************************************** public static async Task AddSuperAdminUser(IServiceProvider services) { // Get UserManager Service to Application UserManager <AppUser> userManager = services.GetRequiredService <UserManager <AppUser> >(); if (!userManager.Users.Any()) { var user = new AppUser { Email = "*****@*****.**", EmailConfirmed = true, UserName = "******", FirstName = "Ahmad", LastName = "Alloush", SelectedLanguages = "ar,en,", RegisterDate = DateTime.Now, Birthday = DateTime.Parse("15/6/1978", MyCultureInfo), PhoneNumber = "515885", PhoneNumberConfirmed = true, AutomatedPassword = false, Address = new Address { Street = "Bahn", BuildingNum = "12", Flore = "L 3", City = "xxxxxxx", Zipcode = "8734", Country = "German" } }; string Password = DefPassword; IdentityResult create = await userManager.CreateAsync(user, Password); if (create.Succeeded) { superAdmin_id = user.Id; userManager.AddToRoleAsync(user, ROLES[0]).Wait(); } var context = services.GetRequiredService <AppDbContext>(); // add default image for SuperAdmin in upload and UserImage tables string[] imgName = { "superAdmin", "superAdminCover", "user (10)", "user (15)" }; for (int i = 0; i < imgName.Length; i++) { var typ = i + 1; var def = true; if (i > 1) { // just the first two images are default typ = 3; def = false; } var upload = new Upload { Name = imgName[i], Path = UploadImageDir + imgName[i] + ".jpg", AddedDateTime = DateTime.Now, UserId = superAdmin_id }; await context.Upload.AddAsync(upload); await context.SaveChangesAsync(); var userImage = new UploadUserImagesList { UploadId = upload.Id, UserId = superAdmin_id, Default = def, UploadTypeId = typ }; await context.UploadUserImagesList.AddAsync(userImage); await context.SaveChangesAsync(); } } }
public async Task <ActionResult <string> > UploadDefaultUserImage([FromForm] IFormFile file, [FromForm] int typeId) { var user = await GetCurrentUserAsync(HttpContext.User); if (user == null) { return(Unauthorized(new ApiResponse(401))); } // 1 = imageProfile, 2 = imageCover if (typeId != 1 && typeId != 2) { return(BadRequest(new ApiResponse(400, "select profile or cover image. 1,2"))); } try { // this path webHostEnvironment.WebRootPath is under wwwroot folder & BLOG_IMAGE_DIRECTORY is where Blog Image Folder string filePath = Path.Combine(_webHostEnvironment.WebRootPath + USER_IMAGE_DIRECTORY); if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } // Create uniqu file name to avoid overwrite old image with same name string fileName = (Guid.NewGuid().ToString().Substring(0, 8)) + "_" + file.FileName; filePath = Path.Combine(filePath, fileName); using (FileStream fileStream = System.IO.File.Create(filePath)) { file.CopyTo(fileStream); // Clears buffers for this stream and causes any buffered data to be written to the file. fileStream.Flush(); var upload = new Upload { Name = fileName, Path = USER_IMAGE_DIRECTORY + fileName, AddedDateTime = DateTime.Now, UserId = user.Id }; await _context.Upload.AddAsync(upload); await _context.SaveChangesAsync(); // change the old default image to false and set new one var defaultImage = await _context.UploadUserImagesList.Where(g => g.UserId == user.Id && g.Default == true && g.UploadTypeId == typeId).FirstOrDefaultAsync(); defaultImage.Default = false; _context.Entry(defaultImage).State = EntityState.Modified; await _context.SaveChangesAsync(); // set the new image var userImage = new UploadUserImagesList { UploadId = upload.Id, UserId = user.Id, UploadTypeId = typeId, Default = true }; await _context.UploadUserImagesList.AddAsync(userImage); await _context.SaveChangesAsync(); return(Ok($"Update Image Successfully")); } } catch (Exception ex) { return(BadRequest(new ApiResponse(400, $"Error: {ex.Message}"))); } }