Beispiel #1
0
        public HttpResponse Add(CardAddModel model)
        {
            var errors = this.validator.ValidateCard(model);

            if (errors.Any())
            {
                return(Error(errors));
            }

            var card = new Card
            {
                Name        = model.Name,
                ImageUrl    = model.Image,
                Keyword     = model.Keyword,
                Description = model.Description,
                Attack      = model.Attack,
                Health      = model.Health,
            };

            this.data.Cards.Add(card);

            var cardId = card.Id.ToString();

            var userCard = new UserCard
            {
                UserId = this.User.Id,
                CardId = int.Parse(cardId)
            };

            this.data.UserCards.Add(userCard);

            this.data.SaveChanges();

            return(Redirect("/Cards/All"));
        }
        // GET: Admin/CustomerCard/Create
        public async Task <IActionResult> Create(int id)
        {
            var Subscriber = await _context.NewSubscribes.
                             Include(s => s.Package).
                             Include(s => s.SetBox).
                             SingleOrDefaultAsync(s => s.Id == id);

            _context.Update(Subscriber);
            Subscriber.Status = await _context.Status.FirstOrDefaultAsync(st => st.Name == "Viewed");

            _context.SaveChanges();

            CardAddModel card = new CardAddModel()
            {
                Id            = id,
                Address       = Subscriber.Address,
                CardNumber    = CardNumberGenerator.CardNumber(),
                ContactNumber = Subscriber.ContactNumber,
                OwnerName     = Subscriber.OwnerName,
                PackageId     = Subscriber.Package.Id,
                SetBoxId      = Subscriber.SetBox.Id,
                SubscribeDate = DateTime.Now
            };

            return(View(card));
        }
        // GET: Admin/CustomerCard/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }
            SetDropDown();
            var customerCard = await _context.CustomerCards.
                               Include(c => c.Package).Include(c => c.SetBox).
                               SingleOrDefaultAsync(m => m.Id == id);

            var model = new CardAddModel
            {
                Address       = customerCard.Address,
                ContactNumber = customerCard.ContactNumber,
                Id            = customerCard.Id,
                OwnerName     = customerCard.OwnerName,
                CardNumber    = customerCard.CardNumber,
                PackageId     = _context.Packages.FirstOrDefault(p => p.Id == customerCard.Package.Id).Id,
                SetBoxId      = _context.SetBoxes.FirstOrDefault(p => p.Id == customerCard.SetBox.Id).Id,
            };

            if (customerCard == null)
            {
                return(NotFound());
            }

            return(View(model));
        }
        public ICollection <string> ValidateCard(CardAddModel card)
        {
            var errors = new List <string>();

            if (card.Name.Length < CardMinName || card.Name.Length > DefaultMaxLength)
            {
                errors.Add("Card name must be between 5 and 20 symbols long.");
            }

            if (card.Description.Length > CardDescriptionMax)
            {
                errors.Add("Description maximum length is 200 symbols.");
            }

            if (card.Health < 0)
            {
                errors.Add("Card health cannot be below zero.");
            }

            if (card.Attack < 0)
            {
                errors.Add("Card attack cannot be below zero.");
            }

            return(errors);
        }
        public async Task <IActionResult> Create([Bind("Id,OwnerName,ContactNumber,Address,CardNumber,SubscribeDate", "PackageId", "SetBoxId")] CardAddModel model)
        {
            if (ModelState.IsValid)
            {
                CustomerCard customerCard = new CustomerCard()
                {
                    Address       = model.Address,
                    SubscribeDate = model.SubscribeDate,
                    CardNumber    = model.CardNumber,
                    ContactNumber = model.ContactNumber,
                    OwnerName     = model.OwnerName,
                    Package       = await _context.Packages.FirstOrDefaultAsync(p => p.Id == model.PackageId),
                    SetBox        = await _context.SetBoxes.FirstOrDefaultAsync(s => s.Id == model.SetBoxId)
                };
                await _context.CustomerCards.AddAsync(customerCard);

                CustomerPackage cp = new CustomerPackage
                {
                    CustomerCard   = customerCard,
                    NumberOfMonths = 0,
                    ExpirationDate = DateTime.Now,
                    Package        = await _context.Packages.FirstOrDefaultAsync(p => p.Id == model.PackageId),
                    Status         = await _context.Status.SingleOrDefaultAsync(s => s.Name == "Recharged")
                };
                await _context.CustomerPackages.AddAsync(cp);

                NewSetBoxRequest request = new NewSetBoxRequest()
                {
                    Card   = customerCard,
                    Setbox = await _context.SetBoxes.FirstOrDefaultAsync(s => s.Id == model.SetBoxId),
                    Status = await _context.Status.SingleOrDefaultAsync(s => s.Name == "AdminApproved")
                };
                await _context.NewSetBoxRequest.AddAsync(request);

                var Subscriber = await _context.NewSubscribes.
                                 Include(s => s.Package).
                                 Include(s => s.SetBox).
                                 SingleOrDefaultAsync(s => s.Id == model.Id);

                _context.Update(Subscriber);
                Subscriber.Status = await _context.Status.FirstOrDefaultAsync(st => st.Name == "AdminApproved");

                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(model));
        }
        public async Task <IActionResult> Edit(int id, [Bind("Id,OwnerName,ContactNumber,Address,CardNumber,SubscribeDate,PackageId,SetBoxId")] CardAddModel model)
        {
            if (id != model.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    CustomerCard customerCard = await _context.CustomerCards.
                                                Include(c => c.Package).Include(c => c.SetBox).
                                                SingleOrDefaultAsync(m => m.Id == id);

                    _context.Update(customerCard);

                    customerCard.OwnerName     = model.OwnerName;
                    customerCard.SubscribeDate = model.SubscribeDate;
                    customerCard.CardNumber    = model.CardNumber;
                    customerCard.ContactNumber = model.ContactNumber;
                    customerCard.Address       = model.Address;
                    customerCard.Package       = _context.Packages.FirstOrDefault(p => p.Id == model.PackageId);
                    customerCard.SetBox        = _context.SetBoxes.FirstOrDefault(s => s.Id == model.SetBoxId);

                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CustomerCardExists(model.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(model));
        }