public bool AddOrUpdate(PictureEntityModel picture) { try { using (var ctx = new DataContext()) { var pictureToUpdate = ctx.Pictures.Where(p => p.id == picture.id) .Include(p => p.User) .Include(p => p.Comments) .Include(p => p.Gallery) .FirstOrDefault(); if (pictureToUpdate != null) { pictureToUpdate.Name = picture.Name; pictureToUpdate.GalleryID = picture.GalleryID; pictureToUpdate.Path = picture.Path; pictureToUpdate.@public = picture.@public; pictureToUpdate.Description = picture.Description; pictureToUpdate.DateEdited = picture.DateEdited; ctx.SaveChanges(); return(true); } else { var newPicture = new PictureEntityModel(); newPicture.Name = picture.Name; newPicture.GalleryID = picture.GalleryID; newPicture.Path = picture.Path; newPicture.@public = picture.@public; newPicture.Description = picture.Description; newPicture.DateEdited = picture.DateEdited; newPicture.DatePosted = picture.DatePosted; newPicture.UserID = picture.UserID; ctx.Pictures.Add(newPicture); ctx.SaveChanges(); return(true); } } } catch (Exception e) { //Handle exceptions } return(false); }
public static PictureViewModel EntityToModel(PictureEntityModel entity) { var model = new PictureViewModel(); model.Name = entity.Name; model.id = entity.id; model.Path = entity.Path; model.UserID = entity.UserID; model.Description = entity.Description; model.DatePosted = entity.DatePosted; model.DateEdited = entity.DateEdited; model.GalleryID = entity.GalleryID; model.IsPublicPicture = entity.@public; model.Uploader = entity.User.FirstName + " " + entity.User.LastName; return(model); }
public static PictureEntityModel ModelToEntity(PictureViewModel model) { var pic = new PictureEntityModel(); pic.Name = model.Name; pic.id = model.id; pic.Path = model.Path; pic.UserID = model.UserID; pic.Description = model.Description; pic.DatePosted = model.DatePosted; pic.DateEdited = model.DateEdited; pic.GalleryID = model.GalleryID; pic.@public = model.IsPublicPicture; return(pic); }