Exemplo n.º 1
0
 public bool LoanBook(int ssn, int copyId)
 {
     using (var dbContextTransaction = _context.Database.BeginTransaction(IsolationLevel.Serializable))
     {
         try
         {
             if (_loaningDa.MemberLoanBooks(ssn, _context) < _memberDa.GetMember(ssn, _context).MemberType.NrOfBooks && //member is allowed to Loan more
                 _loaningDa.GetLoan(copyId, _context) == null && //not Loaned at the time
                 _loaningDa.LoanBook(new Loan {
                 SSN = ssn, CopyID = copyId, FromDate = DateTime.Now
             }, _context))                                                                                        //loan successful
             {
                 dbContextTransaction.Commit();
                 return(true);
             }
             dbContextTransaction.Rollback();
             return(false);
         }
         catch
         {
             dbContextTransaction.Rollback();
             return(false);
         }
     }
 }
Exemplo n.º 2
0
 public int GetAvailableCopyId(string isbn)
 {
     using (var dbContextTransaction = _context.Database.BeginTransaction(IsolationLevel.ReadUncommitted))
     {
         try
         {
             var copies = _copyDa.GetAvailableCopyId(isbn, _context);
             int i      = 0;
             if (copies.Count > 0)
             {
                 while (!false)
                 {
                     if (_loaningDa.GetLoan(copies[i].CopyID, _context) == null)
                     {
                         dbContextTransaction.Commit();
                         return(copies[i].CopyID);
                     }
                     else
                     {
                         i++;
                     }
                 }
             }
             dbContextTransaction.Rollback();
             return(0);
         }
         catch
         {
             dbContextTransaction.Rollback();
             return(0);
         }
     }
 }
        //todo wrong location
        private List <readAllMaterial> CountAvailableCopies(List <readAllMaterial> allMaterials, List <Copy> copies)
        {
            foreach (var readAllMaterial in allMaterials)
            {
                int count = 0;
                foreach (var copy in copies)
                {
                    if (readAllMaterial.ISBN.Equals(copy.ISBN) && readAllMaterial.Location.Equals(copy.LibraryName) &&
                        readAllMaterial.TypeName.Equals(copy.TypeName) &&
                        (_loaningDa.GetLoan(copy.CopyID, _context) == null))
                    {
                        count++;
                    }
                }

                readAllMaterial.Available_Copies = count;
            }
            allMaterials = allMaterials.OrderByDescending(x => x.Available_Copies).ToList();

            return(allMaterials);
        }