Exemplo n.º 1
0
        public void Add(string seats, DateTime fromDate, long restaurantId, string token)
        {
            using (var ctx = new RestAppDbContext())
            {
                var user = TokenHelper.ValidateToken(token, ctx);

                var restaurant = (from r in ctx.Restaurants where r.Id == restaurantId select r).SingleOrDefault();
                if (restaurant == null) throw new FaultException<NotFoundException>(new NotFoundException());
                if (restaurant.Owner.Id != user.Id) throw new FaultException<NotAuthorizedException>(new NotAuthorizedException());
                var correct =
                    !(from p in ctx.Places where p.Restaurant.Id == restaurantId && p.From > fromDate select p).Any();
                if (!correct) throw new FaultException<NotNewestPlaceException>(new NotNewestPlaceException());

                var actualPlace =
                    (from p in ctx.Places where p.Restaurant.Id == restaurantId && p.To == null select p)
                        .SingleOrDefault();

                if (actualPlace != null)
                {
                    actualPlace.To = fromDate;
                }

                ctx.Places.Add(new Place()
                {
                    From = fromDate,
                    Seats = seats,
                    Restaurant = restaurant,
                    To = null
                });

                ctx.SaveChanges();

            }
        }
Exemplo n.º 2
0
        public void Add(long placeId, IEnumerable<Tuple<int, int>> seats, DateTime fromDate, DateTime toDate,
            string token)
        {
            using (var ctx = new RestAppDbContext())
            {
                var user = TokenHelper.ValidateToken(token, ctx);
                var place = (from p in ctx.Places where p.Id == placeId select p).SingleOrDefault();
                if (place == null) throw new FaultException<NotFoundException>(new NotFoundException());
                if (fromDate >= toDate) throw new FaultException<DateOrderException>(new DateOrderException());
                var busy = /*(from r in ctx.Reservations where r.Place.Id ==  placeId && r.To > fromDate || r.From -*/
                    false;
                if (busy) throw new FaultException<SeatsAreBusyException>(new SeatsAreBusyException());

                if (place.From > fromDate || place.To < toDate)
                    throw new FaultException<PlaceDateException>(new PlaceDateException());
                var reservation = new Reservation
                {
                    From = fromDate,
                    To = toDate,
                    Place = place,
                    Guest = user
                };
                ctx.Reservations.Add(reservation);
                var seatsEntity =
                    seats.Select(t => new Seat {Column = t.Item1, Row = t.Item2, Reservation = reservation}).ToList();
                ctx.Seats.AddRange(seatsEntity);
                ctx.SaveChanges();
            }
        }
Exemplo n.º 3
0
 public List<RestaurantDto> GetAllRestaurant(string token)
 {
     using (var ctx = new RestAppDbContext())
     {
         TokenHelper.ValidateToken(token, ctx);
         return  (from r in ctx.Restaurants select r).ToList().Select(RestaurantDto.Convert).ToList();
     }
 }
Exemplo n.º 4
0
 public IEnumerable<PlaceDto> GetPlacesForRestaurantInDate(long restaurantId, DateTime date)
 {
     using (var ctx = new RestAppDbContext())
     {
         return (from p in ctx.Places where p.Restaurant.Id == restaurantId && p.From < date && (p.To > date || p.To == null) select p).ToList()
             .Select(PlaceDto.Convert);
     }
 }
Exemplo n.º 5
0
 public IEnumerable<PlaceDto> GetPlacesForRestaurant(long restaurantId)
 {
     using (var ctx = new RestAppDbContext())
     {
         return (from p in ctx.Places where p.Restaurant.Id == restaurantId select p).ToList()
             .Select(PlaceDto.Convert);
     }
 }
Exemplo n.º 6
0
 public List<RestaurantDto> GetMyRestaurants(string token)
 {
     using (var ctx = new RestAppDbContext())
     {
         var user = TokenHelper.ValidateToken(token, ctx);
         var restaurants = (from r in ctx.Restaurants where r.Owner.Id == user.Id select r).ToList();
         return restaurants.Select(r => RestaurantDto.Convert(r)).ToList();
     }
 }
Exemplo n.º 7
0
 public IEnumerable<ReservationDto> GetForRestaurant(long restaurantId)
 {
     using (var ctx = new RestAppDbContext())
     {
         return
             (from r in ctx.Reservations where r.Place.Restaurant.Id == restaurantId select r)
                 .Include(r => r.Seats)
                 .Include(r => r.Place.Restaurant)
                 .Include(r => r.Guest)
                 .ToList()
                 .Select(ReservationDto.Convert);
     }
 }
Exemplo n.º 8
0
 public void Delete(long reservationId, string token)
 {
     using (var ctx = new RestAppDbContext())
     {
         var user = TokenHelper.ValidateToken(token, ctx);
         var reservation = (from r in ctx.Reservations where r.Id == reservationId select r).SingleOrDefault();
         if (reservation == null) throw new FaultException<NotFoundException>(new NotFoundException());
         if (reservation.Guest.Id != user.Id)
             throw new FaultException<NotAuthorizedException>(new NotAuthorizedException());
         ctx.Reservations.Remove(reservation);
         ctx.SaveChanges();
     }
 }
Exemplo n.º 9
0
 public void EditRestaurant(long id, string name, Guid version, string token)
 {
     using (var ctx = new RestAppDbContext())
     {
         var user = TokenHelper.ValidateToken(token, ctx);
         var restaurantEntity = (from r in ctx.Restaurants where r.Id == id select r).FirstOrDefault();
         if (restaurantEntity == null) throw new FaultException<NotFoundException>(new NotFoundException());
         if(restaurantEntity.RowVersion != version) throw  new FaultException<ConcurrencyException>( new ConcurrencyException());
         if (restaurantEntity.Owner.Id != user.Id) throw new FaultException<NotAuthorizedException>(new NotAuthorizedException());
         restaurantEntity.Name = name;
         ctx.SaveChanges();
     }
 }
Exemplo n.º 10
0
 public void AddRestaurant(string name, string token)
 {
     using (var ctx = new RestAppDbContext())
     {
         var user = TokenHelper.ValidateToken(token, ctx);
         ctx.Restaurants.Add(new Restaurant
         {
             Name = name,
             Owner = user
         });
         ctx.SaveChanges();
     }
 }
Exemplo n.º 11
0
 public UserDto Login(string userName, string password)
 {
     using (var ctx = new RestAppDbContext())
     {
         var user1 =
             (from u in ctx.Users where u.Name == userName && u.Password == password select u);
         var user = user1.SingleOrDefault();
         if (user == null)
         {
             throw new FaultException<BadLoginCredentialsException>(new BadLoginCredentialsException());
         }
         user.Token = Guid.NewGuid().ToString();
         ctx.SaveChanges();
         return UserDto.Convert(user);
     }
 }
Exemplo n.º 12
0
 public void DeleteRestaurant(RestaurantDto restaurant, string token)
 {
     using (var ctx = new RestAppDbContext())
     {
         var user = TokenHelper.ValidateToken(token, ctx);
         var restaurantEntity = (from r in ctx.Restaurants where r.Id == restaurant.Id select r).FirstOrDefault();
         if (restaurantEntity == null) throw new FaultException<NotFoundException>(new NotFoundException());
         if (restaurantEntity.Owner.Id != user.Id) throw new FaultException<NotAuthorizedException>(new NotAuthorizedException());
        /* ctx.Reservations.RemoveRange(restaurantEntity.Reservations);
         foreach (var reservation in restaurantEntity.Reservations)
         {
             reservation.Guest.Reservations.Remove(reservation);
         }
         user.Restaurants.Remove(restaurantEntity);*/
         ctx.Restaurants.Remove(restaurantEntity);
         ctx.SaveChanges();
     }
 }
Exemplo n.º 13
0
 public UserDto RegisterUser(string userName, string password)
 {
     using (var ctx = new RestAppDbContext())
     {
         var user = from u in ctx.Users where u.Name == userName select u;
         if (user.Count() != 0)
         {
             throw new FaultException<AlreadyRegisteredException>(new AlreadyRegisteredException());
         }
         var newUser = new User
         {
             Name = userName,
             Password = password
         };
         ctx.Users.Add(newUser);
         ctx.SaveChanges();
         return UserDto.Convert(newUser);
     }
 }
Exemplo n.º 14
0
 public IEnumerable<ReservationDto> GetForUser(string token)
 {
     using (var ctx = new RestAppDbContext())
     {
         var user = TokenHelper.ValidateToken(token, ctx);
         return
             (from r in ctx.Reservations where r.Guest.Id == user.Id select r)
                 .Include(r => r.Seats)
                 .Include(r => r.Place.Restaurant)
                 .Include(r => r.Guest)
                 .ToList()
                 .Select(ReservationDto.Convert);
     }
 }