Ejemplo n.º 1
0
        public static async Task <bool> BookEvent(User user, Kid kid, ScheduledEvent scheduled)
        {
            Monitor.Enter(coherenceLock);

            bool success = false;

            Event evt       = scheduled.Event;
            User  organizer = evt.Organizer;

            if (user.Credits > evt.Price)
            {
                /*
                 * Transfer funds
                 */
                UpdateQuery <User> chargeQuery = new UpdateQuery <User>();
                chargeQuery.Where("user_id", user.ID);
                chargeQuery.Set("credits", user.Credits - evt.Price);

                int compensation = (int)(evt.Price * (1 - Options.Fee));

                UpdateQuery <User> compensateQuery = new UpdateQuery <User>();
                compensateQuery.Where("user_id", organizer.ID);
                compensateQuery.Set("credits", organizer.Credits + compensation);

                await Program.MySql().ExecuteNonQuery(chargeQuery);

                await Program.MySql().ExecuteNonQuery(compensateQuery);

                /*
                 * Add attendance
                 */
                EventAttendance attendance = new EventAttendance(scheduled, kid);
                await Program.MySql().Insert(attendance);

                success = true;
            }

            Monitor.Exit(coherenceLock);

            return(success);
        }
Ejemplo n.º 2
0
        public async Task BookEvent(HttpRequest req)
        {
            UserSession session = req.Session as UserSession;

            User user = await session.GetUser();

            if (user == null)
            {
                req.SetStatusCode(HttpStatusCode.Forbidden);
                await req.Close();

                return;
            }


            if (!await req.HasPOST("scheduled_id", "kid_id"))
            {
                req.SetStatusCode(HttpStatusCode.BadRequest);
                await req.Close();

                return;
            }


            SelectQuery <ScheduledEvent> scheduledQuery = new SelectQuery <ScheduledEvent>();

            scheduledQuery.Where("scheduled_event_id", await req.POST("scheduled_id"));

            SelectQuery <Kid> kidQuery = new SelectQuery <Kid>();

            kidQuery.Where("kid_id", await req.POST("kid_id"));


            ScheduledEvent scheduled = (await Program.MySql().Execute(scheduledQuery)).FirstOrDefault();
            Kid            kid       = (await Program.MySql().Execute(kidQuery)).FirstOrDefault();

            if (scheduled == null || kid == null)
            {
                await req.SetStatusCode(HttpStatusCode.NotFound).Close();

                return;
            }

            Event evt = scheduled.Event;

            if (user.Credits < evt.Price)
            {
                await req.SetStatusCode(HttpStatusCode.PaymentRequired).Close();

                return;
            }

            if (evt.Organizer.Role != UserRole.Organizer ||
                kid.Age > evt.AgeMax || kid.Age < evt.AgeMin ||
                !(evt.Genders == kid.Gender || (int)evt.Genders > 1))
            {
                await req.SetStatusCode(HttpStatusCode.ExpectationFailed).Close();

                return;
            }

            SelectQuery <EventAttendance> existingAttendance = new SelectQuery <EventAttendance>();

            existingAttendance.Where("scheduled_id", scheduled.ID)
            .Where("kid_id", kid.ID);

            bool success = await Auth.BookEvent(user, kid, scheduled);

            await req.Redirect("/profile");

            BookingEmail email = new BookingEmail(user);
            await email.Body(scheduled, kid, evt.Price);

            await email.Send();
        }
Ejemplo n.º 3
0
 public EventAttendance(ScheduledEvent evt, Kid kid)
 {
     ScheduledID = evt.ID;
     KidID       = kid.ID;
 }