public static void SeedDatabase(this GaloreDbContext _dbContext) { _dbContext.Database.EnsureCreated(); /* ONLY COMMENT OUT WHEN DELETING ALL DATA FROM DB */ // _dbContext.Database.ExecuteSqlCommand("TRUNCATE TABLE Reviews"); // _dbContext.Database.ExecuteSqlCommand("TRUNCATE TABLE Loans"); // _dbContext.Database.ExecuteSqlCommand("TRUNCATE TABLE Users"); // _dbContext.Database.ExecuteSqlCommand("TRUNCATE TABLE Tapes"); /* TAPES */ _dbContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT Tapes ON"); if (!_dbContext.Tapes.Any()) { var tapes = new List <Tape>(); using (StreamReader r = new StreamReader("./JsonData/Tapes_NoId.json")) { string json = r.ReadToEnd(); tapes = JsonConvert.DeserializeObject <List <Tape> >(json); } _dbContext.AddRange(tapes); _dbContext.SaveChanges(); _dbContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT Tapes OFF"); } /* USERS */ _dbContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT Users ON"); if (!_dbContext.Users.Any()) { var users = new List <User>(); using (StreamReader r = new StreamReader("./JsonData/Users_NoId.json")) { string json = r.ReadToEnd(); users = JsonConvert.DeserializeObject <List <User> >(json); } _dbContext.AddRange(users); _dbContext.SaveChanges(); _dbContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT Users OFF"); } _dbContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT Loans ON"); if (!_dbContext.Loans.Any()) { var loans = new List <Loan>(); using (StreamReader r = new StreamReader("./JsonData/Loans.json")) { string json = r.ReadToEnd(); loans = JsonConvert.DeserializeObject <List <Loan> >(json); } _dbContext.AddRange(loans); _dbContext.SaveChanges(); _dbContext.Database.ExecuteSqlCommand("SET IDENTITY_INSERT Loans OFF"); } }
// users/userId/reviews/tapeId : Add new user review for tape public int CreateUserReview(Review review, int userId, int tapeId) { review.UserId = userId; review.TapeId = tapeId; review.DateCreated = DateTime.Now; review.DateModified = DateTime.Now; _context.Reviews.Add(review); _context.SaveChanges(); return(review.Id); }
//Add a new loan into the database public void RegisterTapeOnLoan(int userId, int tapeId) { Loan loan = new Loan { UserId = userId, TapeId = tapeId, BorrowDate = DateTime.Now, ReturnDate = DateTime.MinValue, DateCreated = DateTime.Now, DateModified = DateTime.Now }; _dbContext.Loans.Add(loan); _dbContext.SaveChanges(); }
//add a user to the database and return the user id public int CreateUser(User user) { _dbContext.Users.Add(user); _dbContext.SaveChanges(); return(user.Id); }
//Add a new tape to the database and return the tape id public int CreateTape(Tape tape) { _dbContext.Tapes.Add(tape); _dbContext.SaveChanges(); return(tape.Id); }