public HttpResponseMessage Patch(DateTime diaryId, int id, [FromBody] DiaryEntryModel model) { try { var entity = TheCountingKsRepository.GetDiaryEntry(_identityService.CurrentUser, diaryId, id); if (entity == null) { return(Request.CreateResponse(HttpStatusCode.NotFound)); } var parsedValue = TheModelFactory.Parse(model); if (parsedValue == null) { return(Request.CreateResponse(HttpStatusCode.BadRequest)); } if (entity.Quantity != parsedValue.Quantity) { entity.Quantity = parsedValue.Quantity; if (TheCountingKsRepository.SaveAll()) { return(Request.CreateResponse(HttpStatusCode.OK)); } } return(Request.CreateResponse(HttpStatusCode.BadRequest)); } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }
public IEnumerable <DiaryEntryModel> Get(DateTime diaryId) { var results = TheCountingKsRepository.GetDiaryEntries(_identityService.CurrentUser, diaryId.Date) .ToList() .Select(e => TheModelFactory.Create(e)); return(results); }
public IEnumerable <MeasureV2Model> Get(int foodid) { var results = TheCountingKsRepository.GetMeasuresForFood(foodid) .ToList() .Select(m => TheModelFactory.Create2(m)); return(results); }
public HttpResponseMessage Get(DateTime diaryId, int id) { var result = TheCountingKsRepository.GetDiaryEntry(_identityService.CurrentUser, diaryId.Date, id); if (result == null) { return(Request.CreateResponse(HttpStatusCode.NotFound)); } return(Request.CreateResponse(HttpStatusCode.OK, TheModelFactory.Create(result))); }
public IEnumerable <DiaryModel> Get() { //use Thread.CurrentPrincipal.Identity.Name; but not very testable var username = _identityService.CurrentUser; var results = TheCountingKsRepository.GetDiaries(username) .OrderByDescending(d => d.CurrentDate) .Take(10) .ToList() .Select(d => TheModelFactory.Create(d)); return(results); }
public HttpResponseMessage Get(DateTime diaryid) { var username = _identityService.CurrentUser; var results = TheCountingKsRepository.GetDiary(username, diaryid); if (results == null) { return(Request.CreateResponse(HttpStatusCode.NotFound)); } else { return(Request.CreateResponse(HttpStatusCode.OK, TheModelFactory.Create(results))); } }
public HttpResponseMessage Get(int foodid, int id) { var results = TheCountingKsRepository.GetMeasure(id); if (results == null) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Measure Not Found.")); } if (results.Food.Id == foodid) { return(Request.CreateResponse(HttpStatusCode.Found, TheModelFactory.Create2(results))); } else { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Measure Not Found For Food.")); } }
public HttpResponseMessage Get(DateTime diaryId) { try { var diary = TheCountingKsRepository.GetDiary(_identityService.CurrentUser, diaryId); if (diary == null) { return(Request.CreateResponse(HttpStatusCode.NotFound)); } return(Request.CreateResponse(HttpStatusCode.OK, TheModelFactory.CreateSummary(diary))); } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }
// Use to get an Auth Token // requires that the user has an ApiKey already public HttpResponseMessage Post([FromBody] TokenRequestModel model) { try { var user = TheCountingKsRepository.GetApiUsers().FirstOrDefault(u => u.AppId == model.ApiKey); if (user != null) { var secret = user.Secret; // Simplistic implementation DO NOT USE var key = Convert.FromBase64String(secret); var provider = new System.Security.Cryptography.HMACSHA256(key); // Compute Hash from API Key (NOT SECURE) var hash = provider.ComputeHash(Encoding.UTF8.GetBytes(user.AppId)); var signature = Convert.ToBase64String(hash); if (signature == model.Signature) { var rawTokenInfo = string.Concat(user.AppId + DateTime.UtcNow.ToString("d")); var rawTokenByte = Encoding.UTF8.GetBytes(rawTokenInfo); var token = provider.ComputeHash(rawTokenByte); var authToken = new AuthToken() { Token = Convert.ToBase64String(token), Expiration = DateTime.UtcNow.AddDays(7), ApiUser = user }; if (TheCountingKsRepository.Insert(authToken) && TheCountingKsRepository.SaveAll()) { return(Request.CreateResponse(HttpStatusCode.Created, TheModelFactory.Create(authToken))); } } } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } return(Request.CreateResponse(HttpStatusCode.BadRequest)); }
// Create public HttpResponseMessage Post(DateTime diaryId, [FromBody] DiaryEntryModel model) { //return null; try { var entity = TheModelFactory.Parse(model); if (entity == null) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Could not read diary entry in body")); } var diary = TheCountingKsRepository.GetDiary(_identityService.CurrentUser, diaryId); if (diary == null) { return(Request.CreateResponse(HttpStatusCode.NotFound)); } // Make sure it's not duplicate if (diary.Entries.Any(e => e.Measure.Id == entity.Measure.Id)) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Duplicate Measure not allowed.")); } // Save the new Entry diary.Entries.Add(entity); if (TheCountingKsRepository.SaveAll()) { return(Request.CreateResponse(HttpStatusCode.Created, TheModelFactory.Create(entity))); } else { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Could not save to the database.")); } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }
public HttpResponseMessage Delete(DateTime diaryId, int id) { try { if (TheCountingKsRepository.GetDiaryEntries(_identityService.CurrentUser, diaryId).Any(e => e.Id == id) == false) { return(Request.CreateResponse(HttpStatusCode.NotFound)); } if (TheCountingKsRepository.DeleteDiaryEntry(id) && TheCountingKsRepository.SaveAll()) { return(Request.CreateResponse(HttpStatusCode.OK)); } else { return(Request.CreateResponse(HttpStatusCode.BadRequest)); } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }
// Using Composer in Fiddler, set the return format of the data // use base64encode.org for Authorization credential encoding //http://localhost:23042/api/nutrition/foods?page=3 //User-Agent: Fiddler //Host: localhost:23042 //Content-Type: application/json //Authorization: Basic c2hhd253aWxkZXJtdXRoOnBsdXJhbHNpZ2h0 //[CountingKsAuthorization] public object Get(bool includeMeasures = true, int page = 0) { IQueryable <Food> query; if (includeMeasures) { query = TheCountingKsRepository.GetAllFoodsWithMeasures(); } else { query = TheCountingKsRepository.GetAllFoods(); } var baseQuery = query.OrderBy(f => f.Description); var totalCount = query.Count(); var totalPages = Math.Ceiling((double)totalCount / PAGE_SIZE); var urlHelper = new UrlHelper(Request); var prevPageUrl = page > 0 ? urlHelper.Link("Food", new { page = page - 1 }) : ""; var nextPageUrl = page < totalPages - 1 ? urlHelper.Link("Food", new { page = page + 1 }) : ""; var results = baseQuery .Skip(PAGE_SIZE * page) .Take(PAGE_SIZE) .ToList() .Select(f => TheModelFactory.Create(f)); return(new { TotalCount = totalCount, TotalPages = totalPages, PrevPageUrl = prevPageUrl, NextPageUrl = nextPageUrl, Results = results }); }
public FoodModel Get(int foodid) { return(TheModelFactory.Create(TheCountingKsRepository.GetFood(foodid))); }