Esempio n. 1
0
        //Can be rented could be a solution to the point of a stolen book
        private static void UpdateBook(int id, string book, string author, int?year, IList <string> barcodes, BookingContext db)
        {
            var bookData = db.Books.FirstOrDefault(x => x.BookId == id);

            bookData.Name            = book;
            bookData.Author          = author;
            bookData.PublicationYear = year;

            db.Update(bookData);
            db.SaveChanges();

            var currentBarcodes = db.BooksCopies.Where(x => x.BookId == id).Select(x => x.Barcode).ToList();

            var newBarcodes = barcodes.Except(currentBarcodes).ToList();

            InsertBarcode(newBarcodes, id);

            var removedBarcodes = currentBarcodes.Except(barcodes).ToList();

            foreach (var barcode in removedBarcodes)
            {
                var booksCopiesId = db.BooksCopies.FirstOrDefault(x => x.Barcode == barcode).BooksCopiesId;
                if (db.ReservedBook.Count(x => x.BooksCopiesId == booksCopiesId) == 0)
                {
                    using var db2 = new BookingContext();
                    db2.Remove(new BooksCopies {
                        BooksCopiesId = booksCopiesId
                    });
                    db2.SaveChanges();
                }
            }
        }
Esempio n. 2
0
        public IActionResult CancelReservation(int id)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(RedirectToAction("Index", "Library"));
                }

                using var db = new BookingContext();

                //Uncomment it to send emails
                //var reservedBook = db.ReservedBook.FirstOrDefault(x => x.ReservedBookId == id);
                //var email = db.Users.FirstOrDefault(x => x.UserId == reservedBook.UserId).Email;
                //var book = db.Books.FirstOrDefault(x => x.BookId == reservedBook.BookId).Name;
                //MailLibrary.MailNotifications.SendEmail(email, SUBJECT_CANCEL, string.Format(BODY_CANCEL, book));

                db.Remove(new ReservedBook()
                {
                    ReservedBookId = id
                });
                db.SaveChanges();

                return(RedirectToAction("Index", "Library", new { msg = "reservationCanceled" }));
            }
            catch
            {
                return(RedirectToAction("Index", "Library", new { error = "error" }));
            }
        }
Esempio n. 3
0
        public IActionResult DeleteBook(int id)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(RedirectToAction("Index", "Library"));
                }

                using var db = new BookingContext();

                if (db.ReservedBook.Count(x => x.BookId == id) == 0)
                {
                    db.Remove(new Books()
                    {
                        BookId = id
                    });
                    db.SaveChanges();

                    return(RedirectToAction("Index", "Library", new { msg = "bookDeleted" }));
                }
                return(RedirectToAction("Index", "Library", new { error = "reserved" }));
            }
            catch
            {
                return(RedirectToAction("Index", "Library", new { error = "error" }));
            }
        }
Esempio n. 4
0
        public async Task <IActionResult> Delete(int id)
        {
            var dev = new Book {
                BookId = id
            };

            _context.Remove(dev);
            await _context.SaveChangesAsync();

            return(NoContent());
        }
Esempio n. 5
0
        private static void OldReservations()
        {
            var db = new BookingContext();

            var maxTime = db.Settings.FirstOrDefault().MaxTime;

            foreach (var reservedBook in db.ReservedBook.Where(x => DateTime.Now > x.ReservedDate.AddDays(maxTime)))
            {
                var email = db.Users.FirstOrDefault(x => x.UserId == reservedBook.UserId).Email;

                MailNotifications.SendEmail(email, "Your reservation was auto-cancelled because the book was not collected on time.", "Reservation cancelled");

                db.Remove(new ReservedBook()
                {
                    ReservedBookId = reservedBook.ReservedBookId
                });
                db.SaveChanges();
            }
        }
Esempio n. 6
0
        public IActionResult DeleteUser(int id)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(RedirectToAction("Index", "Library"));
                }

                var user = _httpContextAccessor.HttpContext.Session.GetString("user");
                using var db = new BookingContext();

                if (db.Users.Count(x => x.Role == 0) == 1)
                {
                    return(RedirectToAction("Index", "Library", new { error = "oneAdmin" }));
                }
                else if (id == db.Users.FirstOrDefault(x => x.Username == user).UserId)
                {
                    return(RedirectToAction("Index", "Library", new { error = "sameUser" }));
                }
                else if (db.ReservedBook.Count(x => x.UserId == id) == 0)
                {
                    db.Remove(new Users()
                    {
                        UserId = id
                    });
                    db.SaveChanges();

                    return(RedirectToAction("Index", "Library", new { msg = "userDeleted" }));
                }
                return(RedirectToAction("Index", "Library", new { error = "reserved" }));
            }
            catch
            {
                return(RedirectToAction("Index", "Library", new { error = "error" }));
            }
        }
Esempio n. 7
0
        public IActionResult Cancel(int id)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(RedirectToAction("Index", "Booking"));
                }

                using var db = new BookingContext();

                db.Remove(new ReservedBook()
                {
                    ReservedBookId = id
                });
                db.SaveChanges();

                return(RedirectToAction("Index", "Booking", new { msg = "canceled" }));
            }
            catch
            {
                return(RedirectToAction("Index", "Booking", new { error = "error" }));
            }
        }
Esempio n. 8
0
 public void DeleteOwner(Owner owner)
 {
     _dbContext.Remove(owner);
 }
Esempio n. 9
0
 public void Delete <T>(T entity) where T : class
 {
     _context.Remove(entity);
 }