Beispiel #1
0
        public async Task <IActionResult> AddOrUpdateEat([FromBody] CreateEatRequest eat)
        {
            var loggedUser = User.GetUserIdFromToken();

            await _eatService.AddOrUpdateEatAsync(loggedUser, eat);

            return(Ok());
        }
Beispiel #2
0
        public async Task <IActionResult> AddDish([FromBody] CreateEatRequest eat)
        {
            /*This endpoint is not in use anymore - you should disable it*/
            var loggedUser = User.GetUserIdFromToken();
            var language   = await _userService.GetUserLanguageFromUserIdAsync(loggedUser);

            var result = await _eatService.CreateEatAsync(loggedUser, eat);

            var mapped = _mapper.Map <EatResponse>(result, opt =>
            {
                opt.Items["lang"] = language;
            });

            return(Created("", new ApiOkResponse(mapped)));
        }
Beispiel #3
0
        public async Task <Eat> CreateEatAsync(int loggedUser, CreateEatRequest eat)
        {
            // this assume the user is creating an eat every day
            var eatTypeAlradyExists = await _uow.EatRepository.GetAll()
                                      .Where(e => e.CreatedAt.Date == DateTime.UtcNow.Date && e.EatType == (EatTypeEnum)eat.EatType)
                                      .FirstOrDefaultAsync();

            if (eatTypeAlradyExists != null)
            {
                throw new AlreadyExistsException("An eat is already configured this day.");
            }

            var e = new Eat();

            e.EatType    = (EatTypeEnum)eat.EatType;
            e.CreatedAt  = DateTime.UtcNow;
            e.ModifiedAt = DateTime.UtcNow;
            e.UserId     = loggedUser;
            var eatDishes = new List <EatDish>();

            foreach (var ed in eat.Dishes)
            {
                var eatD = new EatDish();
                eatD.DishId = ed.DishId;
                eatD.Qty    = ed.Qty;

                eatDishes.Add(eatD);
            }
            e.EatDishes = eatDishes;

            await _uow.EatRepository.AddAsync(e);

            await _uow.CommitAsync();

            return(e);
        }
Beispiel #4
0
        public async Task AddOrUpdateEatAsync(int loggedUser, CreateEatRequest eat)
        {
            if (!eat.EatUtcAt.HasValue)
            {
                throw new InvalidDataException("is required", "EatUtcAt");
            }

            var dateInUtc = eat.EatUtcAt.Value;
            // this assume the user is creating an eat of each type every day
            var eatDb = await _uow.EatRepository.GetAll()
                        .Where(e => e.CreatedAt.Date == dateInUtc.Date && e.EatType == (EatTypeEnum)eat.EatType)
                        .FirstOrDefaultAsync();

            //this is an update
            if (eatDb != null)
            {
                eatDb.ModifiedAt = DateTime.UtcNow;

                // delete previous related dishes and compund dishes
                var eatCompoundDishes = eatDb.EatCompoundDishes.ToList();
                var eatDishes         = eatDb.EatDishes.ToList();
                foreach (var item in eatCompoundDishes)
                {
                    _uow.EatCompoundDishRepository.Delete(item);
                }
                foreach (var item in eatDishes)
                {
                    _uow.EatDishRepository.Delete(item);
                }

                // recreate the related dishes objects
                var eatDishesNew = new List <EatDish>();
                foreach (var ed in eat.Dishes)
                {
                    var eatD = new EatDish();
                    eatD.DishId = ed.DishId;
                    eatD.Qty    = ed.Qty;

                    eatDishesNew.Add(eatD);
                }
                eatDb.EatDishes = eatDishesNew;

                var eatCompoundDishesNew = new List <EatCompoundDish>();
                foreach (var ed in eat.CompoundDishes)
                {
                    var eatD = new EatCompoundDish();
                    eatD.CompoundDishId = ed.DishId;
                    eatD.Qty            = ed.Qty;
                    eatCompoundDishesNew.Add(eatD);
                }
                eatDb.EatCompoundDishes = eatCompoundDishesNew;
            }
            // this is a create
            else
            {
                var imcKcals = await GetKCalImcAsync(loggedUser, dateInUtc);

                var kcal = imcKcals.kcal;
                var imc  = imcKcals.imc;
                var e    = new Eat();
                e.EatType          = (EatTypeEnum)eat.EatType;
                e.CreatedAt        = dateInUtc;
                e.ModifiedAt       = DateTime.UtcNow;
                e.UserId           = loggedUser;
                e.EatUtcAt         = eat.EatUtcAt;
                e.KCalAtThatMoment = kcal;
                e.ImcAtThatMoment  = imc;

                var eatDishes = new List <EatDish>();
                foreach (var ed in eat.Dishes)
                {
                    var eatD = new EatDish();
                    eatD.DishId = ed.DishId;
                    eatD.Qty    = ed.Qty;

                    eatDishes.Add(eatD);
                }
                e.EatDishes = eatDishes;

                var eatCompoundDishes = new List <EatCompoundDish>();
                foreach (var ed in eat.CompoundDishes)
                {
                    var eatD = new EatCompoundDish();
                    eatD.CompoundDishId = ed.DishId;
                    eatD.Qty            = ed.Qty;
                    eatCompoundDishes.Add(eatD);
                }
                e.EatCompoundDishes = eatCompoundDishes;

                await _uow.EatRepository.AddAsync(e);
            }
            await _uow.CommitAsync();

            await SetIsBalancedPlanAync(loggedUser, dateInUtc);
        }