Example #1
0
        public HttpResponseMessage Patch(DateTime diaryid, int id, [FromBody] DiaryEntryModel model)
        {
            try
            {
                var entity = TheRepo.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.NotFound));
                }

                if (entity.Quantity != parsedValue.Quantity)
                {
                    entity.Quantity = parsedValue.Quantity;
                    if (TheRepo.SaveAll())
                    {
                        return(Request.CreateResponse(HttpStatusCode.OK));
                    }
                }

                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
        }
Example #2
0
        public HttpResponseMessage Post([FromBody] TokenRequestModel model)
        {
            try
            {
                var user = TheRepo.GetApiUsers().Where(u => u.AppId == model.ApiKey).FirstOrDefault();
                if (user != null)
                {
                    var secret = user.Secret;

                    var key      = Convert.FromBase64String(secret);
                    var provider = new System.Security.Cryptography.HMACSHA256(key);

                    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 (TheRepo.Insert(authToken) && TheRepo.SaveAll())
                        {
                            return(Request.CreateResponse(HttpStatusCode.Created, TheModelFactory.Create(authToken)));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }

            return(Request.CreateResponse(HttpStatusCode.BadRequest));
        }
Example #3
0
 public HttpResponseMessage Delete(DateTime diaryid, int id)
 {
     try
     {
         if (TheRepo.GetDiaryEntries(_identityService.CurrentUser, diaryid).Any(e => e.Id == id) == false)
         {
             return(Request.CreateResponse(HttpStatusCode.NotFound));
         }
         if (TheRepo.DeleteDiaryEntry(id) && TheRepo.SaveAll())
         {
             return(Request.CreateResponse(HttpStatusCode.OK));
         }
         else
         {
             return(Request.CreateResponse(HttpStatusCode.BadRequest));
         }
     }
     catch (Exception ex) {
         return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Could not save in the database"));
     }
 }
Example #4
0
        public HttpResponseMessage Post(DateTime diaryid, [FromBody] DiaryEntryModel model)
        {
            try
            {
                var entity = TheModelFactory.Parse(model);
                if (entity == null)
                {
                    Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Could not read diary entry in body");
                }
                var diary = TheRepo.GetDiary(_identityService.CurrentUser, diaryid);
                if (diary == null)
                {
                    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 (TheRepo.SaveAll())
                {
                    return(Request.CreateResponse(HttpStatusCode.Created, TheModelFactory.Create(entity)));
                }
                else
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Could not save in the database"));
                }
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
        }