Beispiel #1
0
        public HttpResponseMessage Post(DateTime diaryId, [FromBody] DiaryEntryModel diaryEntry)
        {
            try
            {
                var entry = ModelFactory.Parse(diaryEntry);

                if (entry == null)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "error in request body"));
                }

                var diary = CountingKsRepository.GetDiary(_identityService.CurrentUser, diaryId);
                if (diary == null)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "diary does not exist"));
                }

                diary.Entries.Add(entry);
                CountingKsRepository.SaveAll();
                diaryEntry = ModelFactory.Create(entry);

                return(Request.CreateResponse(HttpStatusCode.Created, diaryEntry));
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
        }
Beispiel #2
0
        public HttpResponseMessage Patch(DateTime diaryId, int entryId, [FromBody] DiaryEntryModel diaryEntry)
        {
            try
            {
                if (diaryId == default(DateTime))
                {
                    throw new ArgumentException(nameof(diaryId));
                }

                if (entryId == default(int))
                {
                    throw new ArgumentException(nameof(entryId));
                }

                var entryTmp = ModelFactory.Parse(diaryEntry);
                var entry    = CountingKsRepository.GetDiaryEntry(_identityService.CurrentUser, diaryId, entryId);

                entry.Quantity = entryTmp.Quantity;

                CountingKsRepository.SaveAll();

                return(Request.CreateResponse(HttpStatusCode.OK));
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
        }
Beispiel #3
0
        public HttpResponseMessage Get(DateTime diaryId, int entryId)
        {
            try
            {
                if (diaryId == default(DateTime))
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "diaryId incorrect"));
                }

                if (entryId == default(int))
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "entryId incorrect"));
                }

                var entry = CountingKsRepository.GetDiaryEntry(_identityService.CurrentUser, diaryId, entryId);

                if (entry == null)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "entry not found"));
                }

                return(Request.CreateResponse(HttpStatusCode.OK, ModelFactory.Create(entry)));
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
        }
Beispiel #4
0
    public ActionResult Index()
    {
      var repo = new CountingKsRepository(new CountingKsContext());

      var results = repo.GetAllFoodsWithMeasures().Take(25).ToList();

      return View(results);
    }
        public ActionResult Index()
        {
            var repo = new CountingKsRepository(new CountingKsContext());

            var results = repo.GetAllFoodsWithMeasures().Take(25).ToList();

            return(View(results));
        }
        public IEnumerable <Food> Get()
        {
            var repo = new CountingKsRepository(new CountingKsContext());

            var results = repo.GetAllFoods()
                          .OrderBy(f => f.Description)
                          .Take(25)
                          .ToList();

            return(results);
        }
        public MeasureModel Get(int foodId, int id)
        {
            var measure = CountingKsRepository.GetMeasure(id);

            if (measure.Food.Id == foodId)
            {
                return(ModelFactory.Create(measure));
            }

            return(null);
        }
        public HttpResponseMessage Get(DateTime diaryId)
        {
            var userName = _identityService.CurrentUser;
            var diary    = CountingKsRepository.GetDiary(userName, diaryId);

            if (diary == null)
            {
                return(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            return(Request.CreateResponse(HttpStatusCode.OK,
                                          ModelFactory.Create(CountingKsRepository.GetDiary(userName, diaryId))));
        }
        public object Get(bool includeMeasures = true, int page = 0)
        {
            IQueryable <Food> query;
            var repo = new CountingKsRepository(new CountingKsContext());

            if (includeMeasures)
            {
                query = TheRepository.GetAllFoodsWithMeasures();
            }
            else
            {
                query = TheRepository.GetAllFoods();
            }

            var baseQuery = query.OrderBy(f => f.Description);

            var totalCount = baseQuery.Count();
            var totalPages = Math.Ceiling((double)totalCount / PAGE_SIZE);

            var helper = new UrlHelper(Request);

            var links = new List <LinkModel>();

            if (page > 0)
            {
                links.Add(TheModelFactory.CreateLink(helper.Link("Food", new { page = page - 1 }), "PreviousPage"));
            }

            if (page < totalPages - 1)
            {
                links.Add(TheModelFactory.CreateLink(helper.Link("Food", new { page = page + 1 }), "NextPage"));
            }

            //var prevUrl = page > 0 ? helper.Link("Food", new { page = page - 1}) : "" ;
            //var nextUrl = page < totalPages - 1 ? helper.Link("Food", new { page = page + 1}) : "";

            var results = baseQuery.Skip(PAGE_SIZE * page)
                          .ToList()
                          .Select(f => TheModelFactory.Create(f));



            return(new
            {
                TotalCount = totalCount,
                TotalPages = totalPages,
                Links = links,
                Results = results
            });
        }
        public object Get(bool includeMeasures=true,int page= 0)
        {
            var repo = new CountingKsRepository(new CountingKsContext());

            IQueryable<Food> query;
            if(includeMeasures)
            {
                query = TheRepository.GetAllFoodsWithMeasures();
            }
            else
            {
                query = TheRepository.GetAllFoods();

            }

            var baseQuery = query.OrderBy(f => f.Description);

            var totalCount = baseQuery.Count();
            var totalPages = Math.Ceiling((double)totalCount / PAGE_SIZE); //Math.Ceiling to get 1 more page than needed, so last page is partial page
            var helper = new UrlHelper(Request);

            var links = new List<LinkModel>();

            if(page>0)
            {
                links.Add(TheModelFactory.CreateLink(helper.Link("Food", new { page = page - 1 }),"prevPage"));
            }
            if(page<totalPages -1)
            {
                links.Add(TheModelFactory.CreateLink(helper.Link("Food", new { page = page + 1 }), "nextPage"));
            }

            var results = baseQuery.Skip(PAGE_SIZE * page)
                .Take(25)
                .ToList()
                .Select(f => TheModelFactory.Create(f));

            return new
            {
                TotalCount = totalCount,
                TotalPage = totalPages,
                Links=links,
                Results = results
            };
        }
Beispiel #11
0
        public HttpResponseMessage Get(bool includeMeasures = true, int pageIndex = 0, int pageSize = 0)
        {
            IQueryable <Food> query;

            if (pageIndex < 0)
            {
                pageIndex = 0;
            }

            if (pageSize <= 0)
            {
                pageSize = PAGE_SIZE_DEFAULT;
            }

            if (includeMeasures)
            {
                query = CountingKsRepository.GetAllFoodsWithMeasures();
            }
            else
            {
                query = CountingKsRepository.GetAllFoods();
            }

            query = query.OrderBy(x => x.Description);

            var totalRows = query.Count();

            var result = query
                         .Skip(pageIndex * pageSize)
                         .Take(pageSize)
                         .ToList()
                         .Select(f => ModelFactory.Create(f));

            var pagedResult =
                ModelFactory.CreatePagedResult(includeMeasures, pageIndex, pageSize, totalRows, result, Request);

            return(Request.CreateResponse(HttpStatusCode.OK, pagedResult));
        }
Beispiel #12
0
 public FoodModel Get(int foodId)
 {
     return(ModelFactory.Create(CountingKsRepository.GetFood(foodId)));
 }
        public IEnumerable <DiaryModel> Get()
        {
            var userName = _identityService.CurrentUser;

            return(CountingKsRepository.GetDiaries(userName).ToList().Select(d => ModelFactory.Create(d)));
        }
 public IEnumerable <MeasureModel> Get(int foodId)
 {
     return(CountingKsRepository.GetMeasuresForFood(foodId).ToList().Select(m => ModelFactory.Create(m)));
 }