/// <summary>
        /// cancels a rental ( > delete from db)
        /// </summary>
        /// <param name="rentalId">id of rental to cancel</param>
        /// <returns>true if successful, otherwise false</returns>
        public bool CancelRental(Guid rentalId)
        {
            using (DbContext = new LudothekEntities()) {
                using (var transaction = DbContext.Database.BeginTransaction()) {
                    try {
                        Ausleihe rental = DbContext.Ausleihe.FirstOrDefault(r => r.AusleiheKeyGUID == rentalId);

                        if (rental != null)
                        {
                            rental.Spiel.IsAvailable            = true;
                            DbContext.Entry(rental.Spiel).State = EntityState.Modified;

                            DbContext.Ausleihe.Remove(rental);
                            DbContext.SaveChanges();

                            transaction.Commit();
                            return(true);
                        }
                    } catch (Exception e) {
                        transaction.Rollback();
                        return(false);
                    }
                }
            }
            return(false);
        }
示例#2
0
        /// <summary>
        /// update an existing entity
        /// </summary>
        /// <param name="id">entity id to update</param>
        /// <param name="newEntity">entity with new values</param>
        /// <returns>number of affected rows.</returns>
        public int Update(LudothekEntities context, Guid id, TEntity newEntity)
        {
            context.Entry(newEntity).State = EntityState.Modified;
            var numberOfAffectedRows = context.SaveChanges();

            return(numberOfAffectedRows);
        }
        /// <summary>
        /// creates a new rental obect
        /// </summary>
        /// <param name="customerId">id of customer who rents</param>
        /// <param name="gameId">id of the game to rent</param>
        /// <returns>true if successful, otherwise false</returns>
        public bool AddRental(Guid customerId, Guid gameId)
        {
            using (DbContext = new LudothekEntities())
            {
                using (var transaction = DbContext.Database.BeginTransaction())
                {
                    try
                    {
                        Spiel game     = DbContext.Spiel.FirstOrDefault(g => g.SpielKeyGUID == gameId);
                        Kunde customer = DbContext.Kunde.FirstOrDefault(c => c.KundenKeyGUID == customerId);

                        if (game == null || !game.IsAvailable)
                        {
                            transaction.Dispose();
                            return(false);
                        }

                        Ausleihe rental = new Ausleihe
                        {
                            AusleiheKeyGUID = Guid.NewGuid(),
                            Startdatum      = DateTime.Now,
                            Enddatum        = DateTime.Now.AddDays(7),
                            Kunde           = customer,
                            Spiel           = game
                        };

                        game.IsAvailable            = false;
                        DbContext.Entry(game).State = EntityState.Modified;
                        DbContext.Ausleihe.Add(rental);

                        DbContext.SaveChanges();
                        transaction.Commit();
                        return(true);
                    }
                    catch (Exception e)
                    {
                        transaction.Rollback();
                        return(false);
                    }
                }
            }
        }