public ActionResult Create(Libro libro) { try { libro.Ejemplares = ListaLibEjem; LibroBLL.Create(libro); return RedirectToAction("Index"); } catch(Excepcion ec) { ViewBag.mensaje = new Models.Mensaje(ec.CreateMensaje(), Models.Mensaje.TipoMsg.error); ViewBag.Autores = AutorBLL.ListActivos(); ViewBag.Editoriales = EditorialBLL.ListActivos(); ViewBag.Clasificaciones = ClasificacionBLL.ListActivos(); return View("Create", libro); } catch(Exception ex) { return View("~/Views/Shared/Error.cshtml", new Models.ManejadorError(ex)); } }
public static void Create(Libro libro) { using(var ctx = new BibliotecaContext()) { if(ctx.Libros.Where(l => l.Codigo == libro.Codigo).Count() > 0) throw new Excepcion("Ya existe un registro con el código '" + libro.Codigo + "'"); if(string.IsNullOrEmpty(libro.Edicion)) libro.Edicion = ""; if(string.IsNullOrEmpty(libro.Resumen)) libro.Resumen = ""; if(string.IsNullOrEmpty(libro.Observacion)) libro.Observacion = ""; if(string.IsNullOrEmpty(libro.ISBN)) libro.ISBN = ""; if(string.IsNullOrEmpty(libro.Idioma)) libro.Idioma = ""; List<Ejemplar> le = null; if(libro.Ejemplares != null) { le = libro.Ejemplares.ToList(); validarEjemplares(ctx, le); libro.Ejemplares = null; } Autor a1 = ctx.Autores.Where(a => a.Id == libro.Autor.Id).FirstOrDefault(); Editorial e1 = ctx.Editoriales.Include("Pais").Where(e => e.Id == libro.Editorial.Id).FirstOrDefault(); Clasificacion c1 = ctx.Clasificaciones.Where(c => c.Id == libro.Clasificacion.Id).FirstOrDefault(); libro.Autor = null; libro.Clasificacion = null; libro.Editorial = null; ctx.Libros.AddObject(libro); ctx.SaveChanges(); libro.Autor = a1; libro.Editorial = e1; libro.Clasificacion = c1; foreach(Ejemplar ejemplar in le) { ejemplar.Libro = libro; ctx.Ejemplares.AddObject(ejemplar); } ctx.SaveChanges(); } }
private void FixupLibro(Libro previousValue) { if(previousValue != null && previousValue.Ejemplares.Contains(this)) { previousValue.Ejemplares.Remove(this); } if(Libro != null) { if(!Libro.Ejemplares.Contains(this)) { Libro.Ejemplares.Add(this); } } }
public void registrarLibro() { Libro l = new Libro(); }
public static void Update(Libro libro) { using(var ctx = new BibliotecaContext()) { Libro l1 = ctx.Libros.Where(l => l.Codigo == libro.Codigo).FirstOrDefault(); if(l1 != null && l1.Id != libro.Id) throw new Excepcion("Ya existe un registro con el código '" + libro.Codigo + "'"); if(string.IsNullOrEmpty(libro.Edicion)) libro.Edicion = ""; if(string.IsNullOrEmpty(libro.Resumen)) libro.Resumen = ""; if(string.IsNullOrEmpty(libro.Observacion)) libro.Observacion = ""; if(string.IsNullOrEmpty(libro.ISBN)) libro.ISBN = ""; if(string.IsNullOrEmpty(libro.Idioma)) libro.Idioma = ""; l1 = ctx.Libros.Include("Ejemplares").Where(l => l.Id == libro.Id).FirstOrDefault(); //l1.Ejemplares = null; List<Ejemplar> le1 = null; if(libro.Ejemplares != null) le1 = l1.Ejemplares.ToList(); List<Ejemplar> le2 = null; if(libro.Ejemplares != null) le2 = libro.Ejemplares.ToList(); if(le1 != null) foreach(Ejemplar e2 in le2) { Ejemplar e1 = le1.Where(e => e.Codigo == e2.Codigo || e.Id == e2.Id).FirstOrDefault(); if(e1 != null) e2.Id = e1.Id; } validarEjemplares(ctx, le2); if(le1 != null) foreach(Ejemplar e2 in le2) { Ejemplar e1 = le1.Where(e => e.Codigo == e2.Codigo || e.Id == e2.Id).FirstOrDefault(); if(e1 != null) { e2.Id = e1.Id; e1.CodBarras = e2.CodBarras; e1.Codigo = e2.Codigo; e1.CodRFID = e2.CodRFID; e1.Estado = e2.Estado; e1.TipoPrestamo = e2.TipoPrestamo; e1.Ubicacion = e2.Ubicacion; } } //validando registros eliminados List<Ejemplar> le3 = null; if(le1 != null) { List<int> lId = (le2 == null) ? null : le2.Select(e => e.Id).ToList(); if(lId != null) { le3 = le1.Where(e => e.Id > 0 && !lId.Contains(e.Id)).ToList(); //foreach(Ejemplar e1 in le3) { // if(ctx.Prestamos.Where(p => p.Ejemplares.Where(e => e.Id == e1.Id).FirstOrDefault() != null).FirstOrDefault() != null) // throw new Excepcion("No se puede eliminar el ejemplar '" + e1.Codigo + "', tiene participación en alguna transacción"); //} } } if(le3 != null) foreach(Ejemplar e in le3) ctx.Ejemplares.DeleteObject(e); foreach(Ejemplar e in le2) { if(e.Id < 1) { e.Libro = l1; ctx.Ejemplares.AddObject(e); } } l1.Autor = ctx.Autores.Where(a => a.Id == libro.Autor.Id).FirstOrDefault(); l1.Editorial = ctx.Editoriales.Where(e => e.Id == libro.Editorial.Id).FirstOrDefault(); l1.Clasificacion = ctx.Clasificaciones.Where(c => c.Id == libro.Clasificacion.Id).FirstOrDefault(); l1.Codigo = libro.Codigo; l1.Estado = libro.Estado; l1.ISBN = libro.ISBN; l1.Edicion = libro.Edicion; l1.FechaPublicacion = libro.FechaPublicacion; l1.Idioma = libro.Idioma; l1.Imagen = libro.Imagen; l1.Observacion = libro.Observacion; l1.Resumen = libro.Resumen; l1.Tipo = libro.Tipo; l1.Titulo = libro.Titulo; ctx.SaveChanges(); } }
public static void Delete(Libro libro) { Delete(libro.Id); }