public async Task <IActionResult> Create([Bind("RegistroId,Id,ClaseId")] Registro registro) { var user = await _userManager.GetUserAsync(_httpContextAccessor.HttpContext.User); var userExists = await _context.Registros.FirstOrDefaultAsync(r => (r.ClaseId == registro.ClaseId && r.Id == user.Id)); if (userExists != null) { Alert("Usted ya se ha registrado en este curso", NotificationType.error); return(RedirectToAction(nameof(Index))); } var row = await _context.Clases.FirstOrDefaultAsync(clase => clase.ClaseId == registro.ClaseId); var rows = _context.Registros.Where(r => r.ClaseId == row.ClaseId).ToList(); if (rows.Count >= row.Quantity) { Alert("Lo sentimos, el Curso ha Excedido la Cantidad De Estudiantes", NotificationType.error); return(RedirectToAction(nameof(Index))); } var newRegistro = new Registro { Id = user.Id, ClaseId = registro.ClaseId }; if (ModelState.IsValid) { _context.Add(newRegistro); await _context.SaveChangesAsync(); Alert("Felicitaciones, Te has registrado exitosamente en esta Clase", NotificationType.success); return(RedirectToAction(nameof(Index))); } ViewData["ClaseId"] = new SelectList(_context.Clases, "ClaseId", "Name", registro.ClaseId); ViewData["Id"] = new SelectList(_context.AspNetUsers, "Id", "Id", registro.Id); return(View(registro)); }
public async Task <IActionResult> Create([Bind("ClaseId,Name,Quantity,StartDate,FinishDate,StartHour,EndHour,CursoId,ProfesorId")] Clase clase) { if (ModelState.IsValid) { _context.Add(clase); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["CursoId"] = new SelectList(_context.Cursos, "CursoId", "Name", clase.CursoId); ViewData["ProfesorId"] = new SelectList(_context.Profesores, "ProfesorId", "FirstName", clase.ProfesorId); return(View(clase)); }
public async Task <IActionResult> Create([Bind("ProfesorId,FirstName,LastName,FullName,ImageFile")] Profesor profesor) { if (ModelState.IsValid) { string rootPath = _hostEnviroment.WebRootPath; string fileName = Path.GetFileNameWithoutExtension(profesor.ImageFile.FileName); string extension = Path.GetExtension(profesor.ImageFile.FileName); fileName = fileName + DateTime.Now.ToString("yymmssfff") + extension; profesor.ImageName = fileName; string path = Path.Combine(rootPath + "/assets/images/profesores/", fileName); using (var fileStream = new FileStream(path, FileMode.Create)) { await profesor.ImageFile.CopyToAsync(fileStream); } _context.Add(profesor); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(profesor)); }
public async Task <IActionResult> Create([Bind("CursoId,Name,Type,ImageFile")] Curso curso) { if (ModelState.IsValid) { //Saves images into Courses Image Folder string rootPath = _hostEnviroment.WebRootPath; string fileName = Path.GetFileNameWithoutExtension(curso.ImageFile.FileName); string extension = Path.GetExtension(curso.ImageFile.FileName); fileName = fileName + DateTime.Now.ToString("yymmssfff") + extension; curso.ImageName = fileName; string path = Path.Combine(rootPath + "/assets/images/courses/", fileName); using (var fileStream = new FileStream(path, FileMode.Create)) { await curso.ImageFile.CopyToAsync(fileStream); } _context.Add(curso); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(curso)); }