예제 #1
0
 public ActionResult AddPerformance(Performance perf)
 {
     try
     {
         Picture pic       = new Picture();
         var     p         = Request.Files["Picture"];
         byte[]  imageData = null;
         using (System.IO.BinaryReader br = new System.IO.BinaryReader(p.InputStream))
         {
             imageData = br.ReadBytes(p.ContentLength);
         }
         TMMDbContext db = new TMMDbContext();
         if (imageData.Length != 0)
         {
             pic.Name  = p.FileName.Split('/').Last();
             pic.Image = imageData;
             db.Pictures.Add(pic);
             db.SaveChanges();
             perf.Image = pic;
         }
         db.Performances.Add(perf);
         db.SaveChanges();
         return(RedirectToAction("Index", "Home"));
     }
     catch (Exception e)
     {
         return(RedirectToAction("PrintError", "Home", new { @error = e.GetType() + " " + e.StackTrace + " " + e.Source + " " + e.InnerException.Message }));
     }
 }
        public async Task <IHttpActionResult> BuyTicket()
        {
            string content = await Request.Content.ReadAsStringAsync();

            int         id     = JsonConvert.DeserializeObject <int>(content);
            Ticket      ticket = null;
            Performance perf   = db.Performances.Find(id);

            if (perf != null)
            {
                if (perf.TicketCount > perf.FacticalCount)
                {
                    IEnumerable <Ticket> tickets = db.Tickets.Where(x => x.PerformanceId == perf.Id).OrderBy(x => x.TicketNumber);
                    string UId = User.Identity.GetUserId();
                    int    num = perf.FacticalCount + 1;
                    if (tickets.Count(x => x.TicketNumber == num) > 0)
                    {
                        int prevoius = 0;
                        foreach (Ticket element in tickets)
                        {
                            if (element.TicketNumber != prevoius + 1)
                            {
                                num = prevoius + 1;
                                break;
                            }
                            prevoius++;
                        }
                    }
                    ticket = new Ticket()
                    {
                        PerformanceId = perf.Id,
                        TicketNumber  = num,
                        UserId        = UId
                    };
                    db.Tickets.Add(ticket);
                    perf.FacticalCount++;
                    db.SaveChanges();
                    return(Ok());
                }
                else
                {
                    return(InternalServerError());
                }
            }
            else
            {
                return(InternalServerError());
            }
        }