Ejemplo n.º 1
0
        public async Task <ActionResult <IEnumerable <NutrientGetResponse> > > GetNutrient(string word)
        {
            List <NutrientGetResponse> result = new List <NutrientGetResponse>();

            using (var db = new NutrientContext())
            {
                var dbset = db.nutrients
                            .Where(x => x.approval == true);

                if (word != null)
                {
                    dbset = dbset.Where(x => x.name.Contains(word));
                }
                var ret = await dbset.OrderBy(x => x.name)
                          .ToListAsync();

                foreach (NutrientModel item in ret)
                {
                    NutrientGetResponse nutrient = new NutrientGetResponse();
                    nutrient.id           = item.id;
                    nutrient.name         = item.name;
                    nutrient.protein      = item.protein;
                    nutrient.lipid        = item.lipid;
                    nutrient.carbohydrate = item.carbohydrate;
                    nutrient.calorie      = item.calorie;
                    result.Add(nutrient);
                }
            }
            return(result);
        }
Ejemplo n.º 2
0
        public async Task <ActionResult <AdminPostResponse> > PostAdmin(AdminPostRequest request)
        {
            if (!(await checkToken(request.username, request.token)))
            {
                return(BadRequest());
            }

            AdminPostResponse result = new AdminPostResponse();

            using (var db = new NutrientContext())
            {
                result.nutrients = new List <NutrientGetResponse>();
                IEnumerable <NutrientModel> ret;
                if (request.all == true)
                {
                    ret = await db.nutrients
                          .ToListAsync();
                }
                else
                {
                    ret = await db.nutrients
                          .Where(x => x.approval == true)
                          .ToListAsync();
                }
                foreach (NutrientModel item in ret)
                {
                    NutrientGetResponse nutrient = new NutrientGetResponse();
                    nutrient.id           = item.id;
                    nutrient.name         = item.name;
                    nutrient.protein      = item.protein;
                    nutrient.lipid        = item.lipid;
                    nutrient.carbohydrate = item.carbohydrate;
                    nutrient.calorie      = item.calorie;
                    result.nutrients.Add(nutrient);
                }
                result.token = await updateToken(request.username);
            }
            return(result);
        }