示例#1
0
        //User adds menu
        public async Task AddMenuAsync(UserModel user, MenuModel menu, List <int> mealIds)
        {
            if (user.Position == (int)UserPosition.HasNotHome)
            {
                CustomException errors = new CustomException((int)HttpStatusCode.BadRequest);
                errors.AddError("Home Not Exist", "User is not member of a home");
                errors.Throw();
            }

            if (menu.Date.Kind != DateTimeKind.Utc)
            {
                CustomException errors = new CustomException((int)HttpStatusCode.BadRequest);
                errors.AddError("Date Not Valid", "Date is not valid, please convert to UTC");
                errors.Throw();
            }

            menu.Date = menu.Date.AddHours(-menu.Date.Hour);
            menu.Date = menu.Date.AddMinutes(-menu.Date.Minute);
            menu.Date = menu.Date.AddSeconds(-menu.Date.Second);
            menu.Date = menu.Date.AddMilliseconds(-menu.Date.Millisecond);

            user = await _userRepository.GetByIdAsync(user.Id, true);

            HomeModel home = await _homeRepository.GetByIdAsync(user.Home.Id, true);

            MenuModel tmp = await _menuRepository.GetHomeMenuByDateAsync(home.Id, menu.Date);

            if (tmp != null)
            {
                CustomException errors = new CustomException((int)HttpStatusCode.BadRequest);
                errors.AddError("Date Not Valid", "Date is not valid, that day has already menu");
                errors.Throw();
            }

            mealIds = mealIds.Distinct().ToList();

            if (mealIds.Count == 0)
            {
                CustomException errors = new CustomException((int)HttpStatusCode.BadRequest);
                errors.AddError("Menu Has Not Meal", "There is no meals for this menu");
                errors.Throw();
            }

            menu.Home = home;

            //Finds meals that are not related home
            foreach (var mealId in mealIds)
            {
                MealModel meal = await _mealRepository.GetHomeMealByIdAsync(mealId, true);

                if ((meal == null) || (meal.Home.Id != home.Id))
                {
                    CustomException errors = new CustomException((int)HttpStatusCode.BadRequest);
                    errors.AddError("Meal Not Related Home", "This meal is not related with user home");
                    errors.Throw();
                }
            }

            _menuRepository.Insert(menu);

            //Inserts menu meal to database
            foreach (var mealId in mealIds)
            {
                MealModel meal = await _mealRepository.GetHomeMealByIdAsync(mealId, true);

                MenuMealModel menuMeal = new MenuMealModel(menu, meal);

                _menuMealRepository.Insert(menuMeal);
            }

            //Sends fcm to all users
            foreach (var friend in home.Users)
            {
                FCMModel fcm = new FCMModel(friend.DeviceId, type: "AddMenu");
                fcm.data.Add("Menu", menu);
                fcm.data.Add("MealIds", mealIds);

                await _fcmService.SendFCMAsync(fcm);
            }
        }