Пример #1
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));
            }
        }
Пример #2
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));
            }
        }
        public IHttpActionResult Patch(DateTime diaryId, int entryId, [FromBody] DiaryEntryModel model)
        {
            try
            {
                var diaryEntry = Repository.GetDiaryEntry(_identityService.CurrentUser, diaryId, entryId);
                if (diaryEntry == null)
                {
                    return(NotFound());
                }

                var parsedDiaryEntry = ModelFactory.Parse(model);
                if (parsedDiaryEntry == null)
                {
                    return(BadRequest());
                }

                if (diaryEntry.Quantity != parsedDiaryEntry.Quantity)
                {
                    diaryEntry.Quantity = parsedDiaryEntry.Quantity;
                    if (Repository.Commit())
                    {
                        return(Ok(model));
                    }
                }

                return(BadRequest());
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
        public HttpResponseMessage Patch(DateTime diaryid, int id, [FromBody] DiaryEntryModel model)
        {
            try
            {
                var entity = TheRepository.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 (TheRepository.SaveAll())
                    {
                        return(Request.CreateResponse(HttpStatusCode.OK));
                    }
                }

                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }
            catch (Exception e)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, e.ToString()));
            }
        }
Пример #5
0
        public HttpResponseMessage Patch(DateTime diaryId, int id, [FromBody] DiaryEntryModel model)
        {
            try
            {
                var entry = Repository.GetDiaryEntry(_identityService.CurrentUser, diaryId, id);
                if (entry == null)
                {
                    return(Request.CreateResponse(HttpStatusCode.NotFound, $"A diary entry with id {id} has not been found!"));
                }

                if (entry.Quantity != model.Quantity)
                {
                    entry.Quantity = model.Quantity;
                    return(Repository.SaveAll()
                        ? Request.CreateResponse(HttpStatusCode.OK, ModelFactory.Create(entry))
                        : Request.CreateResponse(HttpStatusCode.BadRequest, "Cannot save changes into database"));
                }

                return(Request.CreateResponse(HttpStatusCode.OK, "Quantity is already the same. Other fields are omit"));
            }
            catch (Exception exc)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, exc));
            }
        }
Пример #6
0
        public HttpResponseMessage Post(DateTime diaryId, [FromBody] DiaryEntryModel model)
        {
            try
            {
                var entry = ModelFactory.Parse(model);

                if (entry == null)
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, "Could not read diary entry in body"));
                }

                var diary = Repository.GetDiary(_identityService.CurrentUser, diaryId);
                if (diary == null)
                {
                    return(Request.CreateResponse(HttpStatusCode.NotFound, $"Cannot find diary entry for {diaryId}"));
                }

                if (diary.Entries.Any(e => e.Measure.Id == entry.Measure.Id))
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, "Duplicate Measure is not allowed!"));
                }

                entry.Diary = diary;
                diary.Entries.Add(entry);
                return(Repository.SaveAll()
                    ? Request.CreateResponse(HttpStatusCode.Created, ModelFactory.Create(entry))
                    : Request.CreateResponse(HttpStatusCode.BadRequest, $"Could not save into database new entry"));
            }
            catch (Exception exc)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest, $"{exc.Message} {exc.InnerException?.Message}"));
            }
        }
        public HttpResponseMessage Post(DateTime diaryId, [FromBody] DiaryEntryModel model)
        {
            try
            {
                var entity = ModelFactory.Parse(model);

                if (entity == null)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Could not read diary entry from body"));
                }

                var diary = Repository.GetDiary(_identityService.CurrentUser, diaryId);
                if (diary == null)
                {
                    return(Request.CreateResponse(HttpStatusCode.NotFound));
                }

                if (diary.Entries.Any(e => e.Measure.Id == entity.Measure.Id))
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Duplicates are not allowed"));
                }

                diary.Entries.Add(entity);
                if (Repository.SaveAll())
                {
                    return(Request.CreateResponse(HttpStatusCode.Created, ModelFactory.Create(entity)));
                }
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Could not save to database"));
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
        }
Пример #8
0
 public IActionResult SetEntry([FromBody] DiaryEntryModel model)
 {
     if (model?.Entry?.Length > Limits.MAX_DIARY_ENTRY)
     {
         throw new InputValueTooLargeException();
     }
     DiaryService.SetEntry(PwdManService, GetToken(), model);
     return(new JsonResult(true));
 }
Пример #9
0
        public HttpResponseMessage Get(DateTime date, int id)
        {
            string     username   = _identityService.CurrentUser;
            DiaryEntry diaryEntry = TheRepository.GetDiaryEntry(username, date, id);

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

            DiaryEntryModel entryModel = TheModelFactory.Create(diaryEntry);

            return(Request.CreateResponse(HttpStatusCode.OK, entryModel));
        }
        public HttpResponseMessage Post(DateTime diaryid, [FromBody] DiaryEntryModel model)
        {
            try
            {
                var entity = TheModelFactory.Parse(model);

                if (entity == null)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Could not read diary entry in body"));
                }

                var diary = TheRepository.GetDiary(_identityService.CurrentUser, diaryid);

                if (diary == null)
                {
                    return(Request.CreateResponse(HttpStatusCode.NotFound));
                }
                //make sure it is not a duplicate.
                //checks if the diary entry that is going to be added to the repository already exists by comparing the measureId of the entity to the existing measureIds in the diary.
                if (diary.Entries.Any(e => e.Measure.Id == entity.Measure.Id))
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Duplicate measures not allowed"));
                }

                diary.UserName = _identityService.CurrentUser;
                //saves the entry.
                diary.Entries.Add(entity);
                if (TheRepository.SaveAll())
                {
                    return(Request.CreateResponse(HttpStatusCode.Created, TheModelFactory.Create(entity)));
                }
                else
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Could Not Save to the Database"));
                }
            }
            catch (Exception e)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, e.ToString()));
            }
        }
        public IHttpActionResult Post(DateTime diaryId, [FromBody] DiaryEntryModel model)
        {
            try
            {
                var diaryEntry = ModelFactory.Parse(model);
                if (diaryEntry == null)
                {
                    return(BadRequest("Could not parse diary entry."));
                }

                var diary = Repository.GetDiary(_identityService.CurrentUser, diaryId);
                if (diary == null)
                {
                    return(BadRequest("Could not read diary in body."));
                }

                if (diary.Entries.Any(e => e.Measure.Id == diaryEntry.Measure.Id))
                {
                    return(BadRequest("Duplicate measure not allowed."));
                }

                diary.Entries.Add(diaryEntry);

                if (!Repository.Commit())
                {
                    return(BadRequest("Could not save to the database."));
                }

                var updatedModel = ModelFactory.Create(diaryEntry);
                var location     = updatedModel.Links
                                   .Select(l => l.Href)
                                   .FirstOrDefault(h => h == SELF_REL);

                return(Created(location, updatedModel));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
Пример #12
0
        public HttpResponseMessage Post(DateTime diaryID, [FromBody] DiaryEntryModel model)
        {
            try
            {
                var entity = TheModelFactory.Parse(model);

                if (entity == null)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Cannot read diary entry from Request Body"));
                }

                var diary = TheRepository.GetDiary(_identityService.CurrentUser, diaryID);

                //check if diary exists
                if (diary == null)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Cannot find diary"));
                }
                //check for duplicate entries
                if (diary.Entries.Any(e => e.Measure.Id == entity.Measure.Id))
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Cannot create diary entry as it already exists would you like to update entry"));
                }

                diary.Entries.Add(entity);
                if (TheRepository.SaveAll())
                {
                    return(Request.CreateResponse(HttpStatusCode.Created, TheModelFactory.Create(entity)));
                }
                else
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Cannot save to db"));
                }
            }
            catch (Exception e)

            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest, "Could not create entity"));
            }
        }
        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 = TheRepository.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 (TheRepository.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));
            }
        }
Пример #14
0
        public HttpResponseMessage Patch(DateTime date, int id, [FromBody] DiaryEntryModel model)
        {
            try
            {
                string     username   = _identityService.CurrentUser;
                DiaryEntry diaryEntry = TheRepository.GetDiaryEntry(username, date, id);

                if (diaryEntry == null)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Can't update a diary entry that does not exist"));
                }

                DiaryEntry newDiaryEntry = TheModelFactory.Parse(model);

                if (newDiaryEntry == null)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Could not read diary entry in body"));
                }

                // Nothing to change
                if (diaryEntry.Quantity == newDiaryEntry.Quantity)
                {
                    return(Request.CreateResponse(HttpStatusCode.OK));
                }

                diaryEntry.Quantity = newDiaryEntry.Quantity;
                if (TheRepository.SaveAll())
                {
                    return(Request.CreateResponse(HttpStatusCode.OK));
                }
                else
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Could not save to the database"));
                }
            }
            catch (Exception exception)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, exception));
            }
        }
Пример #15
0
        public HttpResponseMessage Post(DateTime date, [FromBody] DiaryEntryModel model)
        {
            try
            {
                string     username   = _identityService.CurrentUser;
                DiaryEntry diaryEntry = TheModelFactory.Parse(model);

                if (diaryEntry == null)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Could not read diary entry in body"));
                }

                Diary diary = TheRepository.GetDiary(username, date);

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

                if (diary.Entries.Any(entry => entry.Measure.Id == diaryEntry.Measure.Id))
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Duplicate measure not allowed"));
                }

                diary.Entries.Add(diaryEntry);
                if (TheRepository.SaveAll())
                {
                    return(Request.CreateResponse(HttpStatusCode.Created, TheModelFactory.Create(diaryEntry)));
                }
                else
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Could not save to the database"));
                }
            }
            catch (Exception exception)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, exception));
            }
        }
Пример #16
0
        public HttpResponseMessage Post(DateTime diaryid, [FromBody] DiaryEntryModel model)
        {
            try
            {
                var entity = TheModelFactory.Parse(model);
                if (entity == null)
                {
                    Request.CreateErrorResponse(HttpStatusCode.BadRequest, "cannot found entry");
                }
                var diary = TheRepository.GetDiary(_identityService.CurrentUser, diaryid);
                if (diary == null)
                {
                    Request.CreateResponse(HttpStatusCode.NotFound);
                }
                //check duplicate
                if (diary.Entries.Any(e => e.Measure.Id == entity.Measure.Id))
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, "duplicate"));
                }

                //save new entry
                diary.Entries.Add(entity);
                if (TheRepository.SaveAll())
                {
                    LogActivity.LogSend("post", Request);
                    return(Request.CreateResponse(HttpStatusCode.OK, TheModelFactory.Create(entity)));
                }
                else
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, "not added"));
                }
            }
            catch (Exception exception)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, exception));
            }
        }
Пример #17
0
        public HttpResponseMessage Patch(DateTime diaryId, int Id, [FromBody] DiaryEntryModel model)
        {
            try
            {
                var entity = TheRepository.GetDiaryEntry(_identityService.CurrentUser, diaryId, Id);
                if (entity == null)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Cannot find diary entry"));
                }

                var parsedValue = TheModelFactory.Parse(model);

                if (parsedValue == null)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Cannot parse model to diaryentry in model factory"));
                }

                if (entity.Quantity != parsedValue.Quantity)
                {
                    entity.Quantity = parsedValue.Quantity;

                    if (TheRepository.SaveAll())
                    {
                        return(Request.CreateResponse(HttpStatusCode.OK));
                    }
                }

                //FALLBACK
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }
            catch (Exception e)

            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest, "Could not find entity"));
            }
        }
Пример #18
0
        public void SetEntry(IPwdManService pwdManService, string authenticationToken, DiaryEntryModel model)
        {
            logger.LogDebug("Set diary entry  for {date}...", model.Date);
            var user      = pwdManService.GetUserFromToken(authenticationToken);
            var dbContext = pwdManService.GetDbContext();
            var date      = new DateTime(model.Date.Year, model.Date.Month, model.Date.Day, 0, 0, 0, DateTimeKind.Utc);
            var diary     = dbContext.DbDiaries.SingleOrDefault((d) => d.DbUserId == user.Id && d.Date == date);

            if (diary == null)
            {
                diary = new DbDiary {
                    DbUserId = user.Id, Date = date, Entry = model.Entry
                };
                dbContext.DbDiaries.Add(diary);
            }
            else
            {
                diary.Entry = model.Entry;
            }
            dbContext.SaveChanges();
        }