Esempio n. 1
0
        /// <summary>
        /// Create a venue. A venue can not be created without layouts
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task Create(VenueDto entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException();
            }

            if (!IsNameUnique(entity, true))
            {
                throw new VenueException("Such venue already exists");
            }

            if (entity.LayoutList == null || !entity.LayoutList.Any())
            {
                throw new VenueException("Incorrect state of the venue. The venue must have at least one layout");
            }

            var venueAdd = VenueParser.MapToVenue(entity);

            using (var transaction = CustomTransactionScope.GetTransactionScope())
            {
                _context.VenueRepository.Create(venueAdd);
                await _context.SaveAsync();

                entity.Id = venueAdd.Id;
                foreach (var layout in entity.LayoutList)
                {
                    layout.VenueId = venueAdd.Id;
                    await _layoutService.Create(layout);
                }

                transaction.Complete();
            }
        }
Esempio n. 2
0
        public async Task Create(EventDto entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException();
            }

            if (entity.LayoutId <= 0)
            {
                throw new EventException("LayoutId is invalid");
            }

            if (!IsDateValid(entity, true))
            {
                throw new EventException("Invalid date");
            }

            if (IsPastDate(entity))
            {
                throw new EventException("Attempt of creating event with a date in the past");
            }

            var addEvent = MapToEventDAL(entity);

            _context.EventRepository.Create(addEvent);
            await _context.SaveAsync();

            entity.Id = addEvent.Id;
        }
Esempio n. 3
0
        public async Task AddSeat(int seatId, int userId)
        {
            if (seatId <= 0 || userId <= 0)
            {
                throw new ArgumentException();
            }

            var seat = await _eventSeatService.Get(seatId);

            if (!seat.State.Equals(SeatState.Available))
            {
                throw new CartException("Seat is locked");
            }

            var findCart = await _context.CartRepository.FindByAsync(x => x.UserId == userId);

            var cart = findCart.FirstOrDefault();

            using (var transaction = _context.CreateTransaction())
            {
                try
                {
                    if (cart == null)
                    {
                        var newCart = new Cart
                        {
                            UserId = userId
                        };
                        _context.CartRepository.Create(newCart);
                        await _context.SaveAsync();

                        cart = newCart;
                    }

                    seat.State = SeatState.Ordered;
                    await _eventSeatService.Update(seat);

                    _context.OrderedSeatsRepository.Create(new OrderedSeat
                    {
                        CartId = cart.Id,
                        SeatId = seatId
                    });
                    await _context.SaveAsync();

                    transaction.Commit();
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Create an area. An area can not be created without seats
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task Create(AreaDto entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException();
            }

            if (entity.LayoutId <= 0)
            {
                throw new AreaException("LayoutId is invalid");
            }

            if (!IsDescriptionUnique(entity, true))
            {
                throw new AreaException("Area description isn't unique");
            }

            if (entity.SeatList == null || !entity.SeatList.Any())
            {
                throw new AreaException("Incorrect state of area. An area must have atleast one seat");
            }

            var areaAdd = AreaParser.MapToArea(entity);

            using (var transaction = CustomTransactionScope.GetTransactionScope())
            {
                _context.AreaRepository.Create(areaAdd);
                await _context.SaveAsync();

                entity.Id = areaAdd.Id;
                foreach (var seat in entity.SeatList)
                {
                    seat.AreaId = areaAdd.Id;

                    if (!IsSeatUnique(seat, false))
                    {
                        throw new SeatException("Seat already exists");
                    }

                    var seatAdd = SeatParser.MapToSeat(seat);
                    _context.SeatRepository.Create(seatAdd);
                }
                await _context.SaveAsync();

                transaction.Complete();
            }
        }
Esempio n. 5
0
        public async Task Create(UserDto entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException();
            }

            if (IsUserNameTaken(entity, true))
            {
                throw new UserException("Username is already taken");
            }

            if (IsEmailTaken(entity, true))
            {
                throw new UserException("Email is already taken");
            }

            var addUser = MapToUser(entity);

            using (var transacrion = _context.CreateTransaction())
            {
                try
                {
                    _context.UserRepository.Create(addUser);
                    await _context.SaveAsync();

                    var findRole = await _context.RoleRepository.FindByAsync(x => x.Name.Equals("user", StringComparison.OrdinalIgnoreCase));

                    var role = findRole.FirstOrDefault();

                    if (role == null)
                    {
                        throw new UserException("Role User does not exists");
                    }

                    _context.UserRoleRepository.Create(new UserRole
                    {
                        RoleId = role.Id,
                        UserId = addUser.Id
                    });
                    await _context.SaveAsync();

                    transacrion.Commit();
                    entity.Id = addUser.Id;
                }
                catch (Exception)
                {
                    transacrion.Rollback();
                    throw;
                }
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Create a layout. A layout can not be created without areas
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task Create(LayoutDto entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException();
            }

            if (entity.VenueId <= 0)
            {
                throw new LayoutException("VenueId is invalid");
            }

            if (!IsDescriptionUnique(entity, true))
            {
                throw new LayoutException("Layout description isn't unique");
            }

            if (entity.AreaList == null || !entity.AreaList.Any())
            {
                throw new LayoutException("Incorrect state of the layout. The layout must have at least one area");
            }

            var layoutAdd = LayoutParser.MapToLayout(entity);

            using (var transaction = CustomTransactionScope.GetTransactionScope())
            {
                _context.LayoutRepository.Create(layoutAdd);
                await _context.SaveAsync();

                entity.Id = layoutAdd.Id;
                foreach (var area in entity.AreaList)
                {
                    area.LayoutId = layoutAdd.Id;
                    await _areaService.Create(area);
                }

                transaction.Complete();
            }
        }
Esempio n. 7
0
        public async Task Create(EventAreaDto entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException();
            }

            if (entity.EventId <= 0)
            {
                throw new EventAreaException("EventId is invalid");
            }

            if (!IsDescriptionUnique(entity, true))
            {
                throw new EventAreaException("Area description isn't unique");
            }

            if (entity.Seats == null || !entity.Seats.Any())
            {
                throw new EventAreaException("Invalid state of event area. Seat list is empty");
            }

            var add = MapToEventArea(entity);

            using (var transaction = CustomTransactionScope.GetTransactionScope())
            {
                _context.EventAreaRepository.Create(add);
                await _context.SaveAsync();

                foreach (var seat in entity.Seats)
                {
                    seat.EventAreaId = add.Id;
                    await _seatService.Create(seat);
                }

                transaction.Complete();
            }
            entity.Id = add.Id;
        }
Esempio n. 8
0
        public async Task Create(EventSeatDto entity)
        {
            if (entity == null)
            {
                throw new ArgumentNullException();
            }

            if (entity.EventAreaId <= 0)
            {
                throw new EventSeatException("EventAreaId is invalid");
            }

            if (!IsSeatUnique(entity, true))
            {
                throw new EventSeatException("Seat already exists");
            }

            var add = MapToEventSeat(entity);

            _context.EventSeatRepository.Create(add);
            await _context.SaveAsync();

            entity.Id = add.Id;
        }
Esempio n. 9
0
        public async Task <decimal> Create(int userId)
        {
            if (userId <= 0)
            {
                throw new ArgumentException();
            }

            var orderedSeats = await _cartService.GetOrderedSeats(userId);

            if (!orderedSeats.Any())
            {
                throw new OrderException("User has no ordered seats");
            }

            using (var transaction = _context.CreateTransaction())
            {
                try
                {
                    var user = await _userService.Get(userId);

                    var orderTotal = orderedSeats.Sum(x => x.Area.Price);

                    if (user.Amount < orderTotal)
                    {
                        throw new OrderException("Balance of user is less than total amount of order");
                    }

                    var order = new Order
                    {
                        Date   = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTimeOffset.Now, user.Timezone),
                        UserId = userId
                    };
                    _context.OrderRepository.Create(order);
                    await _context.SaveAsync();

                    foreach (var seat in orderedSeats)
                    {
                        _context.PurchasedSeatRepository.Create(new PurchasedSeat
                        {
                            SeatId  = seat.Seat.Id,
                            OrderId = order.Id,
                            Price   = seat.Area.Price
                        });

                        var updateSeat = await _context.EventSeatRepository.GetAsync(seat.Seat.Id);

                        updateSeat.State = (byte)SeatState.Purchased;
                    }
                    await _context.SaveAsync();

                    user.Amount -= orderTotal;
                    await _userService.Update(user);

                    await _cartService.DeleteUserCart(userId);

                    transaction.Commit();

                    Notify(user, order.Id);

                    return(orderTotal);
                }
                catch (Exception)
                {
                    transaction.Rollback();
                    throw;
                }
            }
        }