public Boolean DeleteComment(Comment deletedComment) { using (var ctx = new MovieNetModelContainer()) { Comment commentToDelete = FindCommentById(deletedComment.Id); if (commentToDelete != null) { ctx.CommentSet.Attach(commentToDelete); ctx.CommentSet.Remove(commentToDelete); try { ctx.SaveChanges(); return(true); } catch (Exception e) { Console.WriteLine("Error deleting Comment."); Console.WriteLine(e.Message); return(false); } } else { Console.WriteLine("Couldn't find Comment to delete with ID " + deletedComment.Id); return(false); } } }
public User UpdateUser(User updatedUser) { using (var ctx = new MovieNetModelContainer()) { User userToUpdate = FindUserByIdPrivate(updatedUser.Id, ctx); if (userToUpdate != null) { userToUpdate.Username = updatedUser.Username; userToUpdate.Password = updatedUser.Password; try { ctx.SaveChanges(); return(updatedUser); } catch { Console.WriteLine("Error updating user."); return(null); } } else { Console.WriteLine("Couldn't find User to update with ID " + updatedUser.Id); return(null); } } }
public Comment UpdateComment(Comment updatedComment) { using (var ctx = new MovieNetModelContainer()) { Comment CommentToUpdate = FindCommentByIdPrivate(updatedComment.Id, ctx); updatedComment.ModificationDate = DateTime.Now; CommentToUpdate.Score = updatedComment.Score; CommentToUpdate.Text = updatedComment.Text; CommentToUpdate.User = updatedComment.User; CommentToUpdate.Film = updatedComment.Film; CommentToUpdate.CreationDate = updatedComment.CreationDate; if (CommentToUpdate != null) { CommentToUpdate = updatedComment; try { ctx.SaveChanges(); } catch { Console.WriteLine("Error updating Comment."); CommentToUpdate = null; } } else { Console.WriteLine("Couldn't find Comment to update with ID " + updatedComment.Id); CommentToUpdate = null; } return(CommentToUpdate); } }
// Methods with one return. It is advised to make only one return on the method. public Film UpdateFilm(Film film) { using (var ctx = new MovieNetModelContainer()) { Film filmToUpdate = FindFilmByIdPrivate(film.Id, ctx); filmToUpdate.Id = film.Id; filmToUpdate.Title = film.Title; filmToUpdate.Genres = film.Genres; filmToUpdate.Synopsis = film.Synopsis; filmToUpdate.Score = film.Score; filmToUpdate.Year = film.Year; filmToUpdate.Director = film.Director; if (filmToUpdate != null) { try { //ctx.Entry(filmToUpdate).State = EntityState.Modified; int num = ctx.SaveChanges(); Console.WriteLine(num); } catch { Console.WriteLine("[FILM DAO] Error updating."); filmToUpdate = null; } } else { filmToUpdate = null; Console.WriteLine("Couldn't update the film " + film.Title); } return(filmToUpdate); } }
public Boolean DeleteUser(User deletedUser) { using (var ctx = new MovieNetModelContainer()) { User userToDelete = FindUserById(deletedUser.Id); if (userToDelete != null) { ctx.UserSet.Attach(userToDelete); ctx.UserSet.Remove(userToDelete); try { ctx.SaveChanges(); return(true); } catch { Console.WriteLine("Error deleting user."); return(false); } } else { Console.WriteLine("Couldn't find User to delete with ID " + deletedUser.Id); return(false); } } }
public User UserValidation(String username, String password) { User user = null; using (var ctx = new MovieNetModelContainer()) try { user = ctx.UserSet.Where(u => u.Username == username && u.Password == password).Single(); } catch (InvalidOperationException) { Console.WriteLine("Context problems"); } return(user); }
public List <Film> FindFilmsByTitle(string title) { using (var ctx = new MovieNetModelContainer()) { List <Film> filmsByTitle = new List <Film>(); try { filmsByTitle = ctx.FilmSet.Where(f => f.Title.Contains(title)).ToList(); } catch (ArgumentNullException) { Console.WriteLine("[FILM DAO] Nothing Found under title :" + title); } return(filmsByTitle); } }
// Find All public List <Film> FindAllFilms() { using (var ctx = new MovieNetModelContainer()) { List <Film> allFilms = new List <Film>(); try { allFilms = ctx.FilmSet.ToList(); } catch (ArgumentNullException) { Console.WriteLine("[FILM DAO] Nothing found."); } return(allFilms); } }
// Find All public List <User> FindAllUsers() { using (var ctx = new MovieNetModelContainer()) { List <User> allUsers = new List <User>(); try { allUsers = ctx.UserSet.ToList(); } catch (ArgumentNullException) { Console.WriteLine("No user found."); } return(allUsers); } }
private Film FindFilmByIdPrivate(int id, MovieNetModelContainer ctx) { Film filmById = null; try { filmById = ctx.FilmSet.Where(f => f.Id.Equals(id)).Single(); } catch (ArgumentNullException) { Console.WriteLine("[FILM DAO] Nothing Found under id:" + id); } catch (InvalidOperationException) { Console.WriteLine("[FILM DAO] Film not found"); } return(filmById); }
// CRUD public Film CreateFilm(Film film) { using (var ctx = new MovieNetModelContainer()) { ctx.FilmSet.Add(film); try { ctx.SaveChanges(); } catch { Console.WriteLine("[FILM DAO] Error executing query"); return(null); } return(film); } }
// ... // CRUD public User CreateUser(User createdUser) { using (var ctx = new MovieNetModelContainer()) { ctx.UserSet.Add(createdUser); try { ctx.SaveChanges(); } catch { Console.WriteLine("Error creating user."); return(null); } return(createdUser); } }
private Comment FindCommentByIdPrivate(int id, MovieNetModelContainer ctx) { Comment CommentById = null; try { CommentById = ctx.CommentSet.Where(c => c.Id == id).Single(); } catch (InvalidOperationException) { Console.WriteLine("Error executing query."); } catch (ArgumentNullException) { Console.WriteLine("Error: could not find Comment with id " + id + "."); } return(CommentById); }
private User FindUserByIdPrivate(int id, MovieNetModelContainer ctx) { User userById = null; try { userById = ctx.UserSet.Where(u => u.Id == id).Single(); } catch (ArgumentNullException) { Console.WriteLine("No user found."); } catch (InvalidOperationException) { Console.WriteLine("Error excuting the query"); } return(userById); }
// ... // CRUD public Comment CreateComment(Comment createdComment) { using (var ctx = new MovieNetModelContainer()) { ctx.CommentSet.Add(createdComment); try { ctx.SaveChanges(); } catch (Exception e) { Console.WriteLine("Error creating Comment."); Console.WriteLine(e.Source); return(null); } return(createdComment); } }
public List <Comment> FindCommentsByFilmId(int filmId) { using (var ctx = new MovieNetModelContainer()) { List <Comment> CommentsByFilmId = new List <Comment>(); try { CommentsByFilmId = ctx.CommentSet.Where(c => c.Film.Id == filmId).ToList(); } catch (ArgumentNullException) { Console.WriteLine("Error executing query."); } catch (InvalidOperationException) { Console.WriteLine("Error: could not find Comments with FilmId " + filmId + "."); } return(CommentsByFilmId); } }
// Find All public List <Comment> FindAllComments() { using (var ctx = new MovieNetModelContainer()) { List <Comment> allComments = new List <Comment>(); try { allComments = ctx.CommentSet.ToList(); } catch (ArgumentNullException) { Console.WriteLine("Could not found any comment"); } catch (InvalidOperationException) { Console.WriteLine("Error executing query."); } return(allComments); } }
// Find by Criteria public User FindUserByUsername(string username) { using (var ctx = new MovieNetModelContainer()) { User userByUsername = null; try { userByUsername = ctx.UserSet.Where(u => u.Username == username).Single(); } catch (InvalidOperationException) { Console.WriteLine("Error executing query."); } catch (ArgumentNullException) { Console.WriteLine("Error: could not find user with username " + username + "."); } return(userByUsername); } }
public Boolean DeleteFilm(Film deletedFilm) { using (var ctx = new MovieNetModelContainer()) { Film filmToDelete = FindFilmByIdPrivate(deletedFilm.Id, ctx); filmToDelete.Id = deletedFilm.Id; filmToDelete.Title = deletedFilm.Title; filmToDelete.Genres = deletedFilm.Genres; filmToDelete.Synopsis = deletedFilm.Synopsis; filmToDelete.Score = deletedFilm.Score; filmToDelete.Year = deletedFilm.Year; filmToDelete.Director = deletedFilm.Director; Boolean isDeleted = false; if (filmToDelete != null) { //ctx.FilmSet.Attach(filmToDelete); ctx.FilmSet.Remove(filmToDelete); try { ctx.SaveChanges(); isDeleted = true; } catch { Console.WriteLine("[FILM DAO] Couldn't delete the film " + filmToDelete.Title); } } else { Console.WriteLine("[FILM DAO] Couldn't find the film " + filmToDelete.Title); } return(isDeleted); } }