public async Task InitializeUserCourse() { await InitializeUser(); await InitializeCourse(); var countusercourse = await _dbContext.UserCourses.CountAsync(); if (countusercourse >= 100) { return; } var timeStamp = new List <TimeSpan>() { TimeSpan.FromHours(3), TimeSpan.FromHours(4), TimeSpan.FromSeconds(30), TimeSpan.FromMinutes(5), TimeSpan.FromMinutes(10), TimeSpan.FromMinutes(50), TimeSpan.FromMinutes(30), };; var userId = await _dbContext.Users.Select(a => a.Id).Take(100).ToListAsync(); var courseId = await _dbContext.Courses.Select(a => a.Id).Take(100).ToListAsync(); var userCourse = new Faker <Data.UserCourse>() .RuleFor(a => a.CourseId, c => c.PickRandom(courseId)) .RuleFor(a => a.UserId, c => c.PickRandom(userId)) .RuleFor(a => a.Duration, c => c.PickRandom(timeStamp)) .Generate(100) ; await _dbContext.UserCourses.AddRangeAsync(userCourse); await _dbContext.SaveChangesAsync(); }
public async Task <IActionResult> UploadCourse() { var file = Request.Form.Files[0]; string folderName = "UploadedFiles"; string webRootPath = _hostingEnvironment.WebRootPath; string newPath = Path.Combine(webRootPath, folderName); if (!Directory.Exists(newPath)) { Directory.CreateDirectory(newPath); } var courses = new List <Course>(); if (file.Length > 0) { string sFileExtension = Path.GetExtension(file.FileName)?.ToLower(); ISheet sheet; string fullPath = Path.Combine(newPath, file.FileName ?? throw new InvalidOperationException()); using (var stream = new FileStream(fullPath, FileMode.Create)) { file.CopyTo(stream); stream.Position = 0; if (sFileExtension == ".xls") { HSSFWorkbook hssfwb = new HSSFWorkbook(stream); //This will read the Excel 97-2000 formats sheet = hssfwb.GetSheetAt(0); //get first sheet from workbook } else { XSSFWorkbook hssfwb = new XSSFWorkbook(stream); //This will read 2007 Excel format sheet = hssfwb.GetSheetAt(0); //get first sheet from workbook } IRow headerRow = sheet.GetRow(0); //Get Header Row int cellCount = headerRow.LastCellNum; for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++) //Read Excel File { IRow row = sheet.GetRow(i); if (row == null) { continue; } if (row.Cells.All(d => d.CellType == CellType.Blank)) { continue; } var course = new Course() { Title = row.GetCell(1).ToString(), Description = row.GetCell(2).ToString(), }; courses.Add(course); } await _context.Courses.AddRangeAsync(courses); await _context.SaveChangesAsync(); return(Ok(new { message = "uploaded successfully" })); } } return(BadRequest("Something went wrong")); }