Example #1
0
        public InfoScreenMutation(
            IAdminRepository admins,
            ILunchplanRepository lunchplans,
            IMealRepository meals,
            IMessageRepository messages
            )
        {
            Name = "Mutation";

            FieldAsync <BooleanGraphType>(
                "createAdmin",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <AdminInputType> > {
                Name = "admin"
            }
                    ),
                resolve: async ctx =>
            {
                var input = ctx.GetArgument <Dictionary <string, object> >("admin");
                var admin = new DAL.Entity.Admin
                {
                    Username = (string)input["username"]
                };
                admin.SetPassword((string)input["password"]);

                return(await admins.CreateAdmin(admin));
            }
                );

            FieldAsync <BooleanGraphType>(
                "updateAdmin",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <AdminInputType> > {
                Name = "admin"
            }
                    ),
                resolve: async ctx =>
            {
                var input = ctx.GetArgument <Dictionary <string, object> >("admin");
                var admin = new DAL.Entity.Admin
                {
                    Username = (string)input["username"]
                };
                admin.SetPassword((string)input["password"]);

                return(await admins.UpdateAdmin(admin));
            }
                );

            FieldAsync <BooleanGraphType>(
                "deleteAdmin",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <StringGraphType> > {
                Name = "admin"
            }
                    ),
                resolve: async ctx =>
            {
                if (ctx.HasArgument("admin"))
                {
                    return(await admins.DeleteAdmin(ctx.GetArgument <string>("admin")));
                }
                return(false);
            });

            FieldAsync <BooleanGraphType>(
                "saveLunchplan",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <LunchplanInputType> > {
                Name = "lunchplan"
            }
                    ),
                resolve: async ctx =>
            {
                var arg      = ctx.GetArgument <Dictionary <string, object> >("lunchplan");
                var week     = (int)arg["week"];
                var mp       = (Dictionary <string, object>)arg["mealplan"];
                var mealplan = new Dictionary <Weekday, int>();
                foreach (var(key, val) in mp)
                {
                    mealplan[Enum.Parse <Weekday>(key, true)] = (int)val;
                }

                var lunchplan = new Lunchplan
                {
                    WeekNumber = week,
                    Mealplan   = mealplan
                };

                return(await lunchplans.SaveLunchplan(lunchplan));
            }
                );

            FieldAsync <BooleanGraphType>(
                "createMeal",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <MealInputType> > {
                Name = "meal"
            }
                    ),
                resolve: async ctx =>
            {
                var meal = ctx.GetArgument <Meal>("meal");
                return(await meals.CreateMeal(meal));
            }
                );

            FieldAsync <BooleanGraphType>(
                "createMessage",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <MessageInputType> > {
                Name = "message"
            }
                    ),
                resolve: async ctx =>
            {
                var message       = ctx.GetArgument <Message>("message");
                message.Date      = DateTime.Now;
                message.CreatedBy = ctx.UserContext.As <InfoScreenUserContext>().AdminId;
                return(await messages.CreateMessage(message));
            }
                );
        }
        /**
         * Updates a Lunchplan for the given week. The plan parameter is expected to be partial, only containing the
         * days that should be updated. All meals in the plan will have their timesChosen field incremented.
         */
        public async Task <bool> SaveLunchplan(Lunchplan plan)
        {
            int  lpId   = plan.Id;
            bool exists = true;

            // if plan.Id is valid, assume it is already present in the db
            if (lpId <= 0)
            {
                // get id of existing lunchplan for the given week, if available
                var existingPlan = await GetRawLunchplan(plan.WeekNumber);

                if (existingPlan == null)
                {
                    await Database.Query(CreateLunchplanQuery, parameters : new Dictionary <string, object>
                    {
                        { "@Week", plan.WeekNumber }
                    });

                    existingPlan = await GetRawLunchplan(plan.WeekNumber);

                    exists = false;
                }

                lpId = existingPlan.Id;
            }

            List <MealVsLunchplan> mvlps;

            if (!exists)
            {
                mvlps = new List <MealVsLunchplan>();
            }
            else
            {
                var mvlpData = await Database.Query(GetMealVsLunchplansQuery, parameters : new Dictionary <string, object>
                {
                    { "@LunchplanId", lpId }
                });

                mvlps = ParseMealsVsLunchplans(mvlpData.Tables["Table"]);
            }

            foreach (Weekday day in Enum.GetValues(typeof(Weekday)))
            {
                int mealId;
                // if an entry is available, update the MVLP for that day
                if (plan.Mealplan.TryGetValue(day, out mealId))
                {
                    // if mealId is set to an invalid ID, remove any existing MVLPs for the day
                    if (mealId <= 0)
                    {
                        // only remove MVLPs if the lunchplan was already in the DB before this function call
                        if (exists)
                        {
                            await Database.Query(RemoveMealVsLunchplanQuery, parameters : new Dictionary <string, object>
                            {
                                { "@LunchplanId", lpId },
                                { "@Weekday", day.ToString() }
                            });
                        }
                    }
                    // valid mealId, either update an existing MVLP or create a new one
                    else
                    {
                        var existing = mvlps.Find(it => it.Weekday == day);
                        if (existing != null)
                        {
                            existing.MealId = mealId;
                            await Database.Query(UpdateMealVsLunchplanQuery, parameters : new Dictionary <string, object>
                            {
                                { "@MealId", mealId },
                                { "@Id", existing.Id }
                            });
                        }
                        else
                        {
                            await Database.Query(CreateMealVsLunchplanQuery, parameters : new Dictionary <string, object>
                            {
                                { "@LunchplanId", lpId },
                                { "@MealId", mealId },
                                { "@Weekday", day.ToString() }
                            });
                        }
                    }
                }
            }

            foreach (var(_, meal) in plan.Mealplan)
            {
                // meal was removed, don't increment
                if (meal == 0)
                {
                    continue;
                }
                await Database.Query(IncrementTimesChosenQuery, parameters : new Dictionary <string, object>
                {
                    { "@Id", meal }
                });
            }

            return(true);
        }