Exemplo n.º 1
0
        public ServiceResponse AddWypozyczenie(WypozyczeniaDTO Wypozyczenie)
        {
            using (var session = NHibernateBase.Session)
            {
                using (var transaction = session.BeginTransaction())
                {
                    try
                    {
                        int ilosc_wyp = 0;
                        int ilosc_rez = 0;
                        var produkty  = session.Query <Produkty>()
                                        .Where(y => y.Id == int.Parse(Wypozyczenie.Produkt_id));

                        foreach (var item in produkty)
                        {
                            foreach (var wyp in session.Query <Wypozyczenia>().Where(x => x.Produkt_id == item.Id.ToString()))
                            {
                                ilosc_wyp = ilosc_wyp + int.Parse(wyp.Ilosc);
                            }
                            foreach (var wyp in session.Query <Rezerwacje>()
                                     .Where(x => x.Produkt_id == item.Id.ToString() && x.Uzytkownik_id != Wypozyczenie.Uzytkownik_id))
                            {
                                ilosc_rez = ilosc_rez + int.Parse(wyp.Ilosc);
                            }
                            if ((int.Parse(item.Ilosc) - (ilosc_wyp + ilosc_rez)) < int.Parse(Wypozyczenie.Ilosc))
                            {
                                throw new Exception("Niewystarczająca ilość produktu.");
                            }
                        }

                        session.Flush();
                        var WypozyczenieAdd = Wypozyczenie.ToWypozyczenia();
                        session.Save(WypozyczenieAdd);
                        transaction.Commit();
                        session.Flush();

                        var query = string.Format("delete {0} where Produkt_id = :Produkt_id and Uzytkownik_id = :Uzytkownik_id", typeof(Rezerwacje));

                        session.CreateQuery(query)
                        .SetParameter("Produkt_id", Wypozyczenie.Produkt_id)
                        .SetParameter("Uzytkownik_id", Wypozyczenie.Uzytkownik_id)
                        .ExecuteUpdate();

                        session.Flush();
                    }
                    catch (Exception e)
                    {
                        transaction.Rollback();
                        return(new ServiceResponse()
                        {
                            Errors = e.StackTrace + " " + e.Message,
                            Success = false
                        });
                    }
                }
            }
            return(new ServiceResponse());
        }
        public static Wypozyczenia ToWypozyczenia(this WypozyczeniaDTO dTO)
        {
            if (dTO == null)
            {
                return(null);
            }

            return(new Wypozyczenia
            {
                Id = dTO.Id,
                Produkt_id = dTO.Produkt_id,
                Uzytkownik_id = dTO.Uzytkownik_id,
                Ilosc = dTO.Ilosc,
                Data_od = dTO.Data_od,
                Ilosc_dni = dTO.Ilosc_dni,
                Data_do = dTO.Data_do
            });
        }