Пример #1
0
        public async Task <ActionResult <int> > PutDailyCount(int grams)
        {
            var user = await userManager.GetUserAsync(User);

            var currentChild = _childrensService.Get().Where(c => c.Id == user.ChildId).FirstOrDefault();

            currentChild.LeucineDailyCount = grams;
            await _childrensService.UpdateAsync(currentChild, currentChild.Id);

            return(grams);
        }
        public async Task <IActionResult> OnPostNewFoodAsync()
        {
            var user = await _userManager.GetUserAsync(User);

            var currentChild = childrensService.Get().Where(c => c.Id == user.ChildId).FirstOrDefault();

            Food.Name = Food.Name.Transform(To.TitleCase);

            await _foodsService.CreateAsync(new Food()
            {
                Created      = DateTime.Now,
                Id           = System.Guid.NewGuid().ToString(),
                Name         = Food.Name.Transform(To.TitleCase),
                ProteinGrams = Food.ProteinGrams,
                Updated      = DateTime.Now,
                WeightGrams  = Food.ServingGrams,
                LastUsed     = DateTime.Now,
                TimesUsed    = 1,
                Manufacturer = ""
            });

            var record = await _recordsService.GetByIdAsync(Food.RecordId);

            var newRecord = new Record()
            {
                Id           = record.Id,
                ChildId      = record.ChildId,
                Created      = record.Created,
                Name         = Food.Name,
                PeriodId     = record.PeriodId,
                ProteinGrams = Food.ProteinGrams,
                Updated      = DateTime.Now,
                WeightGrams  = Food.ServingGrams
            };

            await _recordsService.UpdateAsync(newRecord, user);

            await LoadData();

            ModelState.Clear();
            return(RedirectToPage("/Today"));
        }
        public async Task <ActionResult <Record> > PostRecord(UpdatedRecord record)
        {
            if (string.IsNullOrEmpty(record.Id))
            {
                return(NotFound());
            }

            //here the food to record mapping is done, the actual count conversions are done in the service
            var existing = await _recordsService.GetByIdAsync(record.Id);

            Record newRecord;

            if (!string.IsNullOrEmpty(record.FoodId))
            {
                var food = await _foodsService.GetByIdAsync(record.FoodId);

                newRecord = new Record()
                {
                    Id                = record.Id,
                    ChildId           = existing.ChildId,
                    Created           = existing.Created,
                    LeucineMilligrams = food.LeucineMilligrams,
                    Name              = food.Name,
                    PeriodId          = existing.PeriodId,
                    ProteinGrams      = food.ProteinGrams,
                    Updated           = DateTime.Now,
                    WeightGrams       = food.WeightGrams
                };

                food.TimesUsed += 1;
                food.LastUsed   = DateTime.Now;
                await _foodsService.UpdateAsync(food, food.Id);
            }
            else
            {
                newRecord = new Record()
                {
                    Id                = record.Id,
                    ChildId           = existing.ChildId,
                    Created           = existing.Created,
                    LeucineMilligrams = record.LeucineMilligrams,
                    Name              = existing.Name,
                    PeriodId          = existing.PeriodId,
                    ProteinGrams      = record.ProteinGrams,
                    Updated           = DateTime.Now,
                    WeightGrams       = record.WeightGrams
                };
            }

            var user = await _userManager.GetUserAsync(User);

            var updated = await _recordsService.UpdateAsync(newRecord, user);

            var child        = _childrensService.Get().Where(c => c.Id == user.ChildId).FirstOrDefault();
            var leucineTotal = _recordsService.Get().Where(r => r.Created.Date == user.CurrentView.Date).Sum(r => r.LeucineMilligrams).Value;

            return(new ReturnRecord()
            {
                ChildId = updated.ChildId,
                Created = updated.Created,
                Id = updated.Id,
                LeucineLeft = child.LeucineDailyCount - leucineTotal,
                LeucineMilligrams = updated.LeucineMilligrams,
                LuecineCount = leucineTotal,
                Name = updated.Name,
                PeriodId = updated.PeriodId,
                ProteinGrams = updated.ProteinGrams,
                Updated = updated.Updated,
                WeightGrams = updated.WeightGrams
            });
        }