Controllers/EstudiantesController.cs
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using Microsoft.AspNetCore.Http; | |
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.AspNetCore.Mvc.Rendering; | |
using Microsoft.EntityFrameworkCore; | |
using Microsoft.Extensions.Options; | |
using UniversidadWebsite.Context; | |
using UniversidadWebsite.Helpers; | |
using UniversidadWebsite.Models; | |
namespace UniversidadWebsite.Controllers | |
{ | |
public class EstudiantesController : Controller | |
{ | |
private readonly UniversidadContexto _context; | |
private readonly AzureStorageConfig _config = null; | |
public EstudiantesController(UniversidadContexto context, IOptions<AzureStorageConfig> config) | |
{ | |
_context = context; | |
_config = config.Value; | |
} | |
// GET: Estudiantes | |
public async Task<IActionResult> Index() | |
{ | |
return View(await _context.Estudiantes.ToListAsync()); | |
} | |
// GET: Estudiantes/Details/5 | |
public async Task<IActionResult> Details(int? id) | |
{ | |
if (id == null) | |
{ | |
return NotFound(); | |
} | |
var estudiante = await _context.Estudiantes | |
.FirstOrDefaultAsync(m => m.Id == id); | |
if (estudiante == null) | |
{ | |
return NotFound(); | |
} | |
return View(estudiante); | |
} | |
// GET: Estudiantes/Create | |
public IActionResult Create() | |
{ | |
return View(); | |
} | |
// POST: Estudiantes/Create | |
// To protect from overposting attacks, enable the specific properties you want to bind to. | |
// For more details, see http://go.microsoft.com/fwlink/?LinkId=317598. | |
[HttpPost] | |
[ValidateAntiForgeryToken] | |
public async Task<IActionResult> Create([Bind("Id,Cedula,Nombre,FechaNacimiento,Semestre")] Estudiante estudiante, ICollection<IFormFile> archivo) | |
{ | |
if (ModelState.IsValid) | |
{ | |
var foto = archivo.First(); | |
var nombre = $"{Guid.NewGuid()}.png"; | |
estudiante.Foto = await StorageHelper.SubirArchivo(foto.OpenReadStream(), nombre, _config); | |
_context.Add(estudiante); | |
await _context.SaveChangesAsync(); | |
return RedirectToAction(nameof(Index)); | |
} | |
return View(estudiante); | |
} | |
// GET: Estudiantes/Edit/5 | |
public async Task<IActionResult> Edit(int? id) | |
{ | |
if (id == null) | |
{ | |
return NotFound(); | |
} | |
var estudiante = await _context.Estudiantes.FindAsync(id); | |
if (estudiante == null) | |
{ | |
return NotFound(); | |
} | |
return View(estudiante); | |
} | |
// POST: Estudiantes/Edit/5 | |
// To protect from overposting attacks, enable the specific properties you want to bind to. | |
// For more details, see http://go.microsoft.com/fwlink/?LinkId=317598. | |
[HttpPost] | |
[ValidateAntiForgeryToken] | |
public async Task<IActionResult> Edit(int id, [Bind("Id,Cedula,Nombre,FechaNacimiento,Semestre")] Estudiante estudiante) | |
{ | |
if (id != estudiante.Id) | |
{ | |
return NotFound(); | |
} | |
if (ModelState.IsValid) | |
{ | |
try | |
{ | |
_context.Update(estudiante); | |
await _context.SaveChangesAsync(); | |
} | |
catch (DbUpdateConcurrencyException) | |
{ | |
if (!EstudianteExists(estudiante.Id)) | |
{ | |
return NotFound(); | |
} | |
else | |
{ | |
throw; | |
} | |
} | |
return RedirectToAction(nameof(Index)); | |
} | |
return View(estudiante); | |
} | |
// GET: Estudiantes/Delete/5 | |
public async Task<IActionResult> Delete(int? id) | |
{ | |
if (id == null) | |
{ | |
return NotFound(); | |
} | |
var estudiante = await _context.Estudiantes | |
.FirstOrDefaultAsync(m => m.Id == id); | |
if (estudiante == null) | |
{ | |
return NotFound(); | |
} | |
return View(estudiante); | |
} | |
// POST: Estudiantes/Delete/5 | |
[HttpPost, ActionName("Delete")] | |
[ValidateAntiForgeryToken] | |
public async Task<IActionResult> DeleteConfirmed(int id) | |
{ | |
var estudiante = await _context.Estudiantes.FindAsync(id); | |
_context.Estudiantes.Remove(estudiante); | |
await _context.SaveChangesAsync(); | |
return RedirectToAction(nameof(Index)); | |
} | |
private bool EstudianteExists(int id) | |
{ | |
return _context.Estudiantes.Any(e => e.Id == id); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment