Example #1
0
        public static bool TryLogin(string login, string password)
        {
            using (var context = new LibraryModel())
            {
                var userData = context.Employee.FirstOrDefault(x => x.Login == login);

                if (userData != null)
                {
                    if (ValidatePassword(password, userData.Password))
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
        static public List <BookCopyInfo> ShowBookCopyInfo(string tbCopyID)
        {
            int copyID;

            if (int.TryParse(tbCopyID, out copyID))
            {
                using (var context = new LibraryModel())
                {
                    var copyInfo = from copy in context.Copy
                                   join book in context.Book on copy.ID_Book equals book.ID
                                   where copy.ID == copyID
                                   select new BookCopyInfo {
                        Title = book.Title, PublishDate = book.Release_Date, ISBN = book.ISBN, BookCopyState = copy.Book_State
                    };
                    return(copyInfo.ToList());
                }
            }
            return(null);
        }
        static public List <RentedBooks> ShowRentedBooks(string NrCardText)
        {
            var cardNr = GetCardID(NrCardText);

            if (cardNr == 0)
            {
                return(null);
            }

            using (var context = new LibraryModel())
            {
                var query = from rent in context.Rent
                            where rent.ID_Card == cardNr
                            select new RentedBooks()
                {
                    ID = rent.ID, ReturnDate = rent.Return.Return_Date, RentDate = rent.Rent_Date, RentUntil = rent.Expected_Return_Date, Title = rent.Copy.Book.Title
                };
                return(query.ToList());
            }
        }
        static public List <KeyValuePair <string, int> > GetDataForGraph(string NrCardText)
        {
            var cardNr = GetCardID(NrCardText);

            if (cardNr == 0)
            {
                return(null);
            }

            using (var context = new LibraryModel())
            {
                var sqlMinDate = (DateTime)System.Data.SqlTypes.SqlDateTime.MinValue;
                var query      = from rent in context.Rent
                                 where rent.ID_Card == cardNr
                                 group rent by SqlFunctions.DateAdd("day", SqlFunctions.DateDiff("day", sqlMinDate, rent.Rent_Date), sqlMinDate) into table
                                 select new { date = table.Key.Value, count = table.Count() };

                return(new List <KeyValuePair <string, int> >(query.AsEnumerable().Select(x => new KeyValuePair <string, int>(x.date.ToShortDateString(), x.count)).ToList()));
            }
        }
        static public void Registration(string _FirstName, string _LastName, string _PhoneNumber, string _PostalCode, string _City, string _Street, string _HouseNr, string _ApartmentNr)
        {
            try
            {
                using (var context = new LibraryModel())
                {
                    var card = new Card
                    {
                        First_Name      = _FirstName,
                        Last_Name       = _LastName,
                        Phone_Number    = _PhoneNumber,
                        City            = _City,
                        Postal_Code     = _PostalCode,
                        Street          = _Street,
                        House_Number    = _HouseNr,
                        Apartment_Nuber = _ApartmentNr,
                        State           = 1
                    };

                    var fieldsList = UIChecker.RegisterFieldsDictionary(_FirstName, _LastName, _PhoneNumber, _PostalCode, _City, _Street, _HouseNr);
                    if (UIChecker.CheckForEmptyFields(fieldsList))
                    {
                        ErrorMessageBoxEvent(null, new CustomEventArgs {
                            MessageText = UIChecker.GetEmptyFieldsName(fieldsList)
                        });
                        return;
                    }
                    context.Card.Add(card);
                    context.SaveChanges();
                    InfoMessageBoxEvent(null, new CustomEventArgs {
                        MessageText = $"Registration Successful\nNew ID: {card.ID}", Caption = "Success"
                    });
                }
            }
            catch (Exception)
            {
                ErrorMessageBoxEvent(null, new CustomEventArgs {
                    MessageText = "Unknown error occurred", Caption = "ERROR"
                });
            }
        }
        static public void Return(int ID)
        {
            using (var context = new LibraryModel())
            {
                var rented = (from rent in context.Rent
                              where rent.ID == ID
                              select rent).FirstOrDefault();
                if (rented.Return != null)
                {
                    return;
                }
                var state = rented.Copy.Book_State;
                rented.Return = new Return()
                {
                    State_After_Return = state, State_Pre_Return = state, Return_Date = DateTime.Now
                };


                context.SaveChanges();
            }
        }