Esempio n. 1
0
        public IActionResult RemoveEventFromTop(int eventId, [FromServices] ITopEventServices service)
        {
            TopEvents @event = Context.Tops
                               .FirstOrDefault((arg) => arg.EventId == eventId);

            if (@event == null)
            {
                return(NotFound("Event Not In Top"));
            }

            var tops = Context.Tops.Where((arg) => arg.EventId != eventId)
                       .OrderBy(arg => arg.Index)
                       .ToArray();

            service.ReArrangeTopEvents(tops);

            try {
                Context.Tops.Remove(@event);
                Context.Tops.UpdateRange(tops);
                Context.SaveChanges();
            } catch (DbUpdateException e) {
                return(BadRequest(e.InnerException.Message));
            }

            return(NoContent());
        }
Esempio n. 2
0
        public IActionResult Order(int eventId, int newIndex)
        {
            TopEvents[] topEvents = Context.Tops
                                    .Include(ccc => ccc.Event)
                                    .AsTracking()
                                    .OrderBy((arg) => arg.Index)
                                    .ToArray();

            TopEvents current = topEvents.FirstOrDefault((arg) => arg.EventId == eventId);
            int       diff    = newIndex - current.Index;

            current.Index = newIndex;

            if (current == null)
            {
                return(NotFound("Event not found"));
            }
            if (newIndex > topEvents.Length)
            {
                return(BadRequest("Index out of range"));
            }

            if (diff == 0)
            {
                /* things ordered */
            }
            else if (Math.Abs(diff) == 1)
            {
                topEvents[newIndex - 1].Index -= Math.Sign(diff);
            }
            else
            {
                int cursor = 1;
                for (int i = 0; i < topEvents.Length; i++)
                {
                    if (topEvents[i] == current)
                    {
                        continue;
                    }

                    if (i == newIndex - 1)
                    {
                        cursor = newIndex - Math.Sign(diff);
                    }

                    topEvents[i].Index = cursor;
                    cursor++;
                }
            }

            try {
                Context.Tops.UpdateRange(topEvents);
                Context.SaveChanges();
            } catch (DbUpdateException e) {
                return(BadRequest(e.InnerException.Message));
            }

            return(Ok(topEvents.OrderBy((arg) => arg.Index)));
        }
Esempio n. 3
0
        public async Task <IActionResult> AddTopEvent(int eventId, string name)
        {
            Event @event = await EventServices.GetEventByIdAsync(eventId);

            if (@event == null)
            {
                return(NotFound("Event not found"));
            }
            else if (Context.Tops.Any <TopEvents>((TopEvents arg) => arg.EventId == eventId))
            {
                return(BadRequest("Event has been already add to the top list"));
            }
            else if (@event.Status == Status.DONE)
            {
                return(BadRequest("Event is already done"));
            }

            TopEvents newTopEvent = new TopEvents()
            {
                Event   = @event,
                EventId = @event.Id,
                Index   = Context.Tops.Count() + 1,
                Name    = name
            };

            @event.TopEvents = newTopEvent;

            try {
                Context.Tops.Add(newTopEvent);
                Context.Event.Update(@event);
                Context.SaveChanges();
            } catch (DbUpdateException e) {
                return(BadRequest(e.InnerException.Message));
            }
            return(NoContent());
        }
Esempio n. 4
0
        public async Task ExecuteAsync(CancellationToken cancellationToken)
        {
            if (cancellationToken.IsCancellationRequested)
            {
                return;
            }

            this.PrettyPrint("Start fetching Events Data..", ConsoleColor.Green);

            var optionsBuilder = new DbContextOptionsBuilder <PlannerContext>();

            optionsBuilder.UseMySql(ConnectionString);

            using (var db = new PlannerContext(optionsBuilder.Options)) {
                this.PrettyPrint("Database connection established");

                bool shouldBeProc = await db.Event.AnyAsync(Condition, cancellationToken);

                if (shouldBeProc)
                {
                    Event[] @events = await db.Event.Where(Condition).ToArrayAsync(cancellationToken);

                    foreach (var @event in @events)
                    {
                        var from = @event.Status;

                        if (@event.HasSubscriptionWindow() && @event.SubscribtionOpen())
                        {
                            @event.Status = Status.SUBSCRIPTION;
                        }
                        else if (@event.OnGoingWindow())
                        {
                            @event.Status = Status.ONGOING;
                        }
                        else if (@event.CloseAt >= DateTime.Now &&
                                 ([email protected]() || @event.EndAt <= DateTime.Now))
                        {
                            @event.Status = Status.INCOMING;
                        }
                        else if (@event.CloseAt <= DateTime.Now)
                        {
                            @event.Status = Status.DONE;

                            // Remove top event when event is done
                            int       id  = @event.Id;
                            TopEvents top = db.Tops.FirstOrDefault(e => e.EventId == id);
                            if (top != null)
                            {
                                ITopEventServices service = new TopEventServices(db) as ITopEventServices;
                                TopEvents[]       tops    = db.Tops.Where(e => e.EventId != id)
                                                            .OrderBy(e => e.Index).ToArray();
                                service.ReArrangeTopEvents(tops);

                                db.Tops.Remove(top);
                                db.Tops.UpdateRange(tops);
                            }
                        }
                        else
                        {
                            @event.Status = Status.PENDING;
                        }

                        this.PrettyPrint($"Event: {@event.Id} is updating from: [{from}] to: [{@event.Status}] ");
                    }

                    try {
                        db.Event.UpdateRange(@events);
                        await db.SaveChangesAsync(cancellationToken);
                    } catch (DbUpdateException exception) {
                        this.PrettyPrint($"Error: {exception.Message}", ConsoleColor.Red);
                        throw exception;
                    }

                    this.PrettyPrint($"Event Updated: {@events.Count()}", ConsoleColor.Green);
                }
                else
                {
                    this.PrettyPrint("No Event available for update", ConsoleColor.Cyan);
                }
            }
        }