Beispiel #1
0
        public IHttpActionResult RemoveJournalEntryTag(int journalEntryTagId)
        {
            using (var db = new DiabetesFoodJournalContext())
            {
                var journalEntryTagFromDb = db.JournalEntryTags.FirstOrDefault(x => x.Id == journalEntryTagId);

                if (journalEntryTagFromDb != null)
                {
                    db.JournalEntryTags.Remove(journalEntryTagFromDb);
                    db.SaveChanges();
                }
            }

            return(Ok());
        }
Beispiel #2
0
        public IHttpActionResult SaveJournalEntry([FromBody] JournalEntryModel entry)
        {
            try
            {
                if (entry == null)
                {
                    throw new ArgumentNullException(nameof(entry));
                }

                using (var db = new DiabetesFoodJournalContext())
                {
                    try
                    {
                        db.Database.BeginTransaction();
                        var entryFromDb = db.JournalEntries.FirstOrDefault(x => x.Id == entry.Id);

                        if (entryFromDb == null)
                        {
                            entryFromDb = db.JournalEntries.Create();
                        }

                        entryFromDb.Logged = entry.Logged;
                        entryFromDb.Notes  = entry.Notes ?? "";
                        entryFromDb.Title  = entry.Title;

                        if (entryFromDb.Id == 0)
                        {
                            db.JournalEntries.Add(entryFromDb);
                            db.SaveChanges();
                            entry.Id = entryFromDb.Id;
                        }

                        var doseFromDb = db.Doses.FirstOrDefault(x => x.Id == entry.Dose.Id);

                        if (doseFromDb == null)
                        {
                            doseFromDb = db.Doses.Create();
                        }

                        doseFromDb.Extended      = entry.Dose.Extended;
                        doseFromDb.InsulinAmount = entry.Dose.InsulinAmount;
                        doseFromDb.TimeExtended  = entry.Dose.TimeExtended;
                        doseFromDb.TimeOffset    = entry.Dose.TimeOffset;
                        doseFromDb.UpFront       = entry.Dose.UpFront;

                        if (doseFromDb.Id == 0)
                        {
                            db.Doses.Add(doseFromDb);
                            db.SaveChanges();
                            entry.Dose.Id = doseFromDb.Id;
                        }

                        var entryDoseFromDb = db.JournalEntryDoses.FirstOrDefault(x => x.JournalEntryId == entryFromDb.Id && x.DoseId == doseFromDb.Id);

                        if (entryDoseFromDb == null)
                        {
                            entryDoseFromDb = db.JournalEntryDoses.Create();

                            entryDoseFromDb.JournalEntryId = entryFromDb.Id;
                            entryDoseFromDb.DoseId         = doseFromDb.Id;

                            db.JournalEntryDoses.Add(entryDoseFromDb);
                        }

                        var nutritionalInfoFromDb = db.NutritionalInfos.FirstOrDefault(x => x.Id == entry.NutritionalInfo.Id);

                        if (nutritionalInfoFromDb == null)
                        {
                            nutritionalInfoFromDb = db.NutritionalInfos.Create();
                        }

                        nutritionalInfoFromDb.Protein       = entry.NutritionalInfo.Protein;
                        nutritionalInfoFromDb.Calories      = entry.NutritionalInfo.Calories;
                        nutritionalInfoFromDb.Carbohydrates = entry.NutritionalInfo.Carbohydrates;

                        if (nutritionalInfoFromDb.Id == 0)
                        {
                            db.NutritionalInfos.Add(nutritionalInfoFromDb);
                            db.SaveChanges();
                            entry.NutritionalInfo.Id = nutritionalInfoFromDb.Id;
                        }

                        var entryNutritionalInfoFromDb = db.JournalEntryNutritionalInfos.FirstOrDefault(x => x.JournalEntryId == entryFromDb.Id && x.NutritionalInfoId == nutritionalInfoFromDb.Id);

                        if (entryNutritionalInfoFromDb == null)
                        {
                            entryNutritionalInfoFromDb = db.JournalEntryNutritionalInfos.Create();

                            entryNutritionalInfoFromDb.JournalEntryId    = entryFromDb.Id;
                            entryNutritionalInfoFromDb.NutritionalInfoId = nutritionalInfoFromDb.Id;

                            db.JournalEntryNutritionalInfos.Add(entryNutritionalInfoFromDb);
                        }

                        foreach (var tag in entry.Tags)
                        {
                            var tagFromDb = db.Tags.FirstOrDefault(x => x.Id == tag.Id);

                            if (tagFromDb == null)
                            {
                                tagFromDb = db.Tags.Create();
                            }

                            tagFromDb.Description = tag.Description;

                            if (tagFromDb.Id == 0)
                            {
                                db.Tags.Add(tagFromDb);
                                db.SaveChanges();
                            }

                            var journalEntryTagFromDb = db.JournalEntryTags.FirstOrDefault(x => x.JournalEntryId == entryFromDb.Id && x.TagId == tagFromDb.Id);

                            if (journalEntryTagFromDb == null)
                            {
                                journalEntryTagFromDb = db.JournalEntryTags.Create();

                                journalEntryTagFromDb.JournalEntryId = entryFromDb.Id;
                                journalEntryTagFromDb.TagId          = tagFromDb.Id;
                                db.JournalEntryTags.Add(journalEntryTagFromDb);
                            }
                        }

                        db.SaveChanges();
                        db.Database.CurrentTransaction.Commit();
                    }
                    catch (Exception e)
                    {
                        db.Database.CurrentTransaction.Rollback();
                        if (e.InnerException != null)
                        {
                            return(BadRequest(e.InnerException.Message));
                        }
                        else
                        {
                            return(BadRequest(e.Message));
                        }
                    }
                }

                return(Ok(entry));
            }
            catch (ArgumentNullException e)
            {
                return(BadRequest(e.Message));
            }
            catch (Exception e)
            {
                if (e.InnerException != null)
                {
                    return(BadRequest(e.InnerException.Message));
                }
                else
                {
                    return(BadRequest(e.Message));
                }
            }
        }
Beispiel #3
0
        public IHttpActionResult SaveTag([FromBody] Tag tag)
        {
            try
            {
                if (tag == null)
                {
                    throw new ArgumentNullException(nameof(tag));
                }

                using (var db = new DiabetesFoodJournalContext())
                {
                    try
                    {
                        var tagFromDb = db.Tags.FirstOrDefault(x => x.Id == tag.Id);

                        if (tagFromDb == null)
                        {
                            tagFromDb = db.Tags.Create();
                        }

                        tagFromDb.Description = tag.Description;

                        if (tag.Id == 0)
                        {
                            db.Tags.Add(tagFromDb);
                        }

                        db.SaveChanges();
                        tag.Id = tagFromDb.Id;
                    }
                    catch (Exception e)
                    {
                        db.Database.CurrentTransaction.Rollback();
                        if (e.InnerException != null)
                        {
                            return(BadRequest(e.InnerException.Message));
                        }
                        else
                        {
                            return(BadRequest(e.Message));
                        }
                    }
                }

                return(Ok(tag.Id));
            }
            catch (ArgumentNullException e)
            {
                return(BadRequest(e.Message));
            }
            catch (Exception e)
            {
                if (e.InnerException != null)
                {
                    return(BadRequest(e.InnerException.Message));
                }
                else
                {
                    return(BadRequest(e.Message));
                }
            }
        }