public void PostInsertsEntry(string userName) { var projectionStub = new Mock <IUserNameProjection>(); var queryDummy = new Mock <IJournalEntriesQuery>(); var cmdMock = new Mock <IAddJournalEntryCommand>(); var sut = new JournalController( projectionStub.Object, queryDummy.Object, cmdMock.Object) { Request = new HttpRequestMessage() }; sut.Request.Properties.Add( HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration()); projectionStub.Setup(p => p.GetUserName(sut.Request)).Returns(userName); var entry = new JournalEntryModel(); sut.Post(entry); cmdMock.Verify(c => c.AddJournalEntry(entry, userName)); }
public async void Post_XmlJournalEntryModelObject_ResponseIsSuccess() { var journalEntryModel = new JournalEntryModel() { Distance = 3000, Duration = new TimeSpan(1, 25, 56), Time = new DateTimeOffset(DateTime.Today), // Born = DateTime.Today, }; string json = JsonConvert.SerializeObject(journalEntryModel); //arrange using (var client = TestHttpClientFactory.Create("JournalController")) { //act // var response = client.PostAsJsonAsync().Result; client.DefaultRequestHeaders.Accept.ParseAdd("application/xml"); var response = await client.PostAsJsonAsync("", json); var returnPerson = await response.Content.ReadAsAsync <JournalEntryModel>(); //assert Assert.That(response.IsSuccessStatusCode, Is.True, "response:" + response); //Assert.That(returnPerson.Id, Is.GreaterThan(0)); } }
public void PostWithoutUserNameReturnsCorrectResponse() { var projectionStub = new Mock <IUserNameProjection>(); var queryDummy = new Mock <IJournalEntriesQuery>(); var cmdMock = new Mock <IAddJournalEntryCommand>(); var sut = new JournalController( projectionStub.Object, queryDummy.Object, cmdMock.Object) { Request = new HttpRequestMessage() }; sut.Request.Properties.Add( HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration()); projectionStub .Setup(p => p.GetUserName(It.IsAny <HttpRequestMessage>())) .Returns((string)null); var dummyEntry = new JournalEntryModel(); var response = sut.Post(dummyEntry); Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode); cmdMock.Verify( c => c.AddJournalEntry(It.IsAny <JournalEntryModel>(), It.IsAny <string>()), Times.Never); }
public ActionResult JournalEntry(string calendarDate) { JournalEntryModel JournalModel = journalRepo.GetTrainingLogDetails(calendarDate); // return Redirect("/") return(View(JournalModel)); }
public ActionResult JournalEntry(string calendarDate, JournalEntryModel model) { journalRepo.SaveTrainingLogDetails(model); JournalEntryModel JournalModel = journalRepo.GetTrainingLogDetails(calendarDate); // return Redirect("/") // return View(model); return(View(JournalModel)); }
public JournalEntryModel Build(JournalEntry journalEntry) { var currentEntry = new JournalEntryModel(); currentEntry.Tags = new List <TagModel>(); currentEntry.Id = journalEntry.Id; currentEntry.Logged = journalEntry.Logged; currentEntry.Notes = journalEntry.Notes; currentEntry.Title = journalEntry.Title; if (journalEntry.JournalEntryNutritionalInfos != null && journalEntry.JournalEntryNutritionalInfos.Any()) { var nutrition = journalEntry.JournalEntryNutritionalInfos.FirstOrDefault().NutritionalInfo; if (nutrition.Id > 0) { currentEntry.NutritionalInfo = new NutritionalInfoModel(); currentEntry.NutritionalInfo.Id = nutrition.Id; currentEntry.NutritionalInfo.Calories = nutrition.Calories; currentEntry.NutritionalInfo.Carbohydrates = nutrition.Carbohydrates; currentEntry.NutritionalInfo.Protein = nutrition.Protein; } } if (journalEntry.JournalEntryDoses != null && journalEntry.JournalEntryDoses.Any()) { var dose = journalEntry.JournalEntryDoses.FirstOrDefault().Dose; if (dose.Id > 0) { currentEntry.Dose = new DoseModel(); currentEntry.Dose.Id = dose.Id; currentEntry.Dose.Extended = dose.Extended; currentEntry.Dose.InsulinAmount = dose.InsulinAmount; currentEntry.Dose.TimeExtended = dose.TimeExtended; currentEntry.Dose.TimeOffset = dose.TimeOffset; currentEntry.Dose.UpFront = dose.UpFront; } } if (journalEntry.JournalEntryTags != null && journalEntry.JournalEntryTags.Any()) { foreach (var tag in journalEntry.JournalEntryTags) { var tagViewModel = new TagModel(); tagViewModel.Id = tag.Tag.Id; tagViewModel.Description = tag.Tag.Description; currentEntry.Tags.Add(tagViewModel); } } return(currentEntry); }
public IHttpActionResult Create([FromBody] JournalEntryModel journalEntry) { if (journalEntry == null) { return(BadRequest()); } try { _journalEntries.Create(journalEntry); return(Ok()); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public void AddJournalEntry(JournalEntryModel journal, string userName) { var userId = this.db.User .FindAllByUserName(userName) .Select(this.db.User.UserId) .ToScalarOrDefault <int>(); if (userId == 0) { userId = this.db.User.Insert(UserName: userName).UserId; } this.db.JournalEntry.Insert( UserId: userId, Time: journal.Time, Distance: journal.Distance, Duration: journal.Duration); }
public async void Post_JsonJournalEntryModelObject_ResponseIsSuccess() { var journalEntryModel = new JournalEntryModel() { Distance = 3000, Duration = new TimeSpan(1,25,56), Time = new DateTimeOffset(DateTime.Today), // Born = DateTime.Today, }; //string json2 = JsonConvert.SerializeObject(journalEntryModel); var json = new { time = DateTimeOffset.Now, distance = 8500, duration = TimeSpan.FromMinutes(44) }; //arrange using (var client = TestHttpClientFactory.Create("JournalController")) { //act // var response = client.PostAsJsonAsync().Result; client.DefaultRequestHeaders.Accept.ParseAdd("application/json"); //2. Accept headers in the request //client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var response = client.PostAsJsonAsync("", journalEntryModel).Result; // var returnPerson = await response.Content.ReadAsAsync<JournalEntryModel>(); //assert Assert.That(response.IsSuccessStatusCode, Is.True, "response:" + response); //Assert.That(returnPerson.Id, Is.GreaterThan(0)); } }
/// <summary> /// Creates a journal entry to the database. /// </summary> /// <param name="journalEntry">A model that contains the journal entry info /// to be created.</param> public void Create(JournalEntryModel journalEntry) { // Prepare the object var journalEntryObj = (SAPbobsCOM.JournalEntries)_company.GetBusinessObject(BoObjectTypes.oJournalEntries); // Set header values journalEntryObj.Series = journalEntry.Series; journalEntryObj.TaxDate = DateTime.Now; journalEntryObj.ReferenceDate = DateTime.Now; journalEntryObj.DueDate = DateTime.Now; // Set line values foreach (var line in journalEntry.Lines) { if (line != journalEntry.Lines.First()) { journalEntryObj.Lines.Add(); } journalEntryObj.Lines.AccountCode = line.AccountCode; journalEntryObj.Lines.TaxCode = line.TaxCode; journalEntryObj.Lines.FCCredit = line.FCCredit; journalEntryObj.Lines.FCDebit = line.FCDebit; journalEntryObj.Lines.Credit = line.Credit; journalEntryObj.Lines.Debit = line.Debit; } // Add it to database var success = journalEntryObj.Add().Equals(0); if (!success) { // Error handling int code; string msg; _company.GetLastError(out code, out msg); throw new Exception($"Something went wrong\n{code} {msg}"); } Marshal.ReleaseComObject(journalEntryObj); }
public HttpResponseMessage Post(JournalEntryModel journalEntry) { SimpleWebToken swt; SimpleWebToken.TryParse(this.Request.Headers.Authorization.Parameter, out swt); var userName = swt.Single(c => c.Type == "userName").Value; var connStr = ConfigurationManager.ConnectionStrings["running-journal"].ConnectionString; var db = Database.OpenConnection(connStr); var userId = db.User.Insert(UserName: userName).UserId; db.JournalEntry.Insert( UserId: userId, Time: journalEntry.Time, Distance: journalEntry.Distance, Duration: journalEntry.Duration ); return(this.Request.CreateResponse()); }
public async void Post_JsonJournalEntryModelObject_ResponseIsSuccess() { var journalEntryModel = new JournalEntryModel() { Distance = 3000, Duration = new TimeSpan(1, 25, 56), Time = new DateTimeOffset(DateTime.Today), // Born = DateTime.Today, }; //string json2 = JsonConvert.SerializeObject(journalEntryModel); var json = new { time = DateTimeOffset.Now, distance = 8500, duration = TimeSpan.FromMinutes(44) }; //arrange using (var client = TestHttpClientFactory.Create("JournalController")) { //act // var response = client.PostAsJsonAsync().Result; client.DefaultRequestHeaders.Accept.ParseAdd("application/json"); //2. Accept headers in the request //client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var response = client.PostAsJsonAsync("", journalEntryModel).Result; // var returnPerson = await response.Content.ReadAsAsync<JournalEntryModel>(); //assert Assert.That(response.IsSuccessStatusCode, Is.True, "response:" + response); //Assert.That(returnPerson.Id, Is.GreaterThan(0)); } }
/// <summary> /// Gets the details of a journal entry. /// </summary> /// <param name="jdtNum">The number of the journal entry to be returned.</param> /// <returns>A model that represents the journal entry info.</returns> public JournalEntryModel GetById(int jdtNum) { // Prepare the object var journalEntryObj = (SAPbobsCOM.JournalEntries)_company.GetBusinessObject(BoObjectTypes.oJournalEntries); JournalEntryModel journalEntry = null; if (journalEntryObj.GetByKey(jdtNum)) { journalEntry = new JournalEntryModel(); journalEntry.Number = journalEntryObj.Number; journalEntry.Series = journalEntryObj.Series; journalEntry.TaxDate = journalEntryObj.TaxDate; journalEntry.ReferenceDate = journalEntryObj.ReferenceDate; journalEntry.DueDate = journalEntryObj.DueDate; // TODO: Check if it works. for (int i = 0; i < journalEntryObj.Lines.Count; i++) { journalEntryObj.SetCurrentLine(i); journalEntry.Lines.Add( new JournalEntryLineModel { AccountCode = journalEntryObj.Lines.AccountCode, TaxCode = journalEntryObj.Lines.TaxCode, FCCredit = journalEntryObj.Lines.FCCredit, FCDebit = journalEntryObj.Lines.FCDebit, Credit = journalEntryObj.Lines.Credit, Debit = journalEntryObj.Lines.Debit }); } } Marshal.ReleaseComObject(journalEntryObj); return(journalEntry); }
public async Task <ActionResult <JournalEntry> > PostJournalEntry([FromBody] JournalEntryModel entry, [FromQuery] int userId) { try { if (entry == null) { throw new ArgumentNullException(nameof(entry)); } { try { this.context.Database.BeginTransaction(); var userExists = await this.context.Users.AnyAsync(x => x.Id == userId); if (userExists) { var userJournalEntryFromDb = await this.context.UserJournalEntries.FirstOrDefaultAsync(x => x.UserId == userId && x.JournalEntryId == entry.Id); if (userJournalEntryFromDb == null) { userJournalEntryFromDb = new UserJournalEntry(); } userJournalEntryFromDb.UserId = userId; var entryFromDb = await this.context.JournalEntries.FirstOrDefaultAsync(x => x.Id == entry.Id); if (entryFromDb == null) { entryFromDb = new JournalEntry(); } entryFromDb.Logged = entry.Logged; entryFromDb.Notes = entry.Notes ?? ""; entryFromDb.Title = entry.Title; if (entryFromDb.Id == 0) { this.context.JournalEntries.Add(entryFromDb); this.context.SaveChanges(); entry.Id = entryFromDb.Id; } userJournalEntryFromDb.JournalEntryId = entry.Id; if (userJournalEntryFromDb.Id == 0) { this.context.UserJournalEntries.Add(userJournalEntryFromDb); } var doseFromDb = this.context.Doses.FirstOrDefault(x => x.Id == entry.Dose.Id); if (doseFromDb == null) { doseFromDb = new Dose(); } 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) { this.context.Doses.Add(doseFromDb); this.context.SaveChanges(); entry.Dose.Id = doseFromDb.Id; } var entryDoseFromDb = this.context.JournalEntryDoses.FirstOrDefault(x => x.JournalEntryId == entryFromDb.Id && x.DoseId == doseFromDb.Id); if (entryDoseFromDb == null) { entryDoseFromDb = new JournalEntryDose(); entryDoseFromDb.JournalEntryId = entryFromDb.Id; entryDoseFromDb.DoseId = doseFromDb.Id; this.context.JournalEntryDoses.Add(entryDoseFromDb); } var nutritionalInfoFromDb = this.context.NutritionalInfos.FirstOrDefault(x => x.Id == entry.NutritionalInfo.Id); if (nutritionalInfoFromDb == null) { nutritionalInfoFromDb = new NutritionalInfo(); } nutritionalInfoFromDb.Protein = entry.NutritionalInfo.Protein; nutritionalInfoFromDb.Calories = entry.NutritionalInfo.Calories; nutritionalInfoFromDb.Carbohydrates = entry.NutritionalInfo.Carbohydrates; if (nutritionalInfoFromDb.Id == 0) { this.context.NutritionalInfos.Add(nutritionalInfoFromDb); this.context.SaveChanges(); entry.NutritionalInfo.Id = nutritionalInfoFromDb.Id; } var entryNutritionalInfoFromDb = this.context.JournalEntryNutritionalInfos.FirstOrDefault(x => x.JournalEntryId == entryFromDb.Id && x.NutritionalInfoId == nutritionalInfoFromDb.Id); if (entryNutritionalInfoFromDb == null) { entryNutritionalInfoFromDb = new JournalEntryNutritionalInfo(); entryNutritionalInfoFromDb.JournalEntryId = entryFromDb.Id; entryNutritionalInfoFromDb.NutritionalInfoId = nutritionalInfoFromDb.Id; this.context.JournalEntryNutritionalInfos.Add(entryNutritionalInfoFromDb); } var journalEntryTagsFromDb = this.context.JournalEntryTags.Where(x => x.JournalEntryId == entryFromDb.Id); foreach (var dbTag in journalEntryTagsFromDb.ToList()) { if (entry.Tags.Where(x => x.Id == dbTag.TagId).Any() == false) { this.context.Remove(dbTag); this.context.SaveChanges(); } } foreach (var tag in entry.Tags) { var tagFromDb = this.context.Tags.FirstOrDefault(x => x.Id == tag.Id); if (tagFromDb == null) { tagFromDb = new Tag(); } tagFromDb.Description = tag.Description; if (tagFromDb.Id == 0) { this.context.Tags.Add(tagFromDb); this.context.SaveChanges(); } var journalEntryTagFromDb = this.context.JournalEntryTags.FirstOrDefault(x => x.JournalEntryId == entryFromDb.Id && x.TagId == tagFromDb.Id); if (journalEntryTagFromDb == null) { journalEntryTagFromDb = new JournalEntryTag(); journalEntryTagFromDb.JournalEntryId = entryFromDb.Id; journalEntryTagFromDb.TagId = tagFromDb.Id; this.context.JournalEntryTags.Add(journalEntryTagFromDb); } } this.context.SaveChanges(); this.context.Database.CurrentTransaction.Commit(); } } catch (Exception e) { this.context.Database.CurrentTransaction.Rollback(); if (e.InnerException != null) { return(BadRequest(e.InnerException.Message)); } else { return(BadRequest(e.Message)); } } } return(CreatedAtAction("GetJournalEntry", new { id = entry.Id }, 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)); } } }
public HttpResponseMessage Post(JournalEntryModel journalEntry) { entries.Add(journalEntry); return(this.Request.CreateResponse()); }
/// <summary> /// Will convert a dbModel to its DTO pair. /// </summary> /// <param name="dbModel">The Journal Entry that we are converting to a DTO.</param> /// <returns>The DTO that represent the journal entry provided.</returns> public static JournalEntryModel LoadJournalEntry(JournalEntry dbModel) { JournalEntryModel ret = null; switch (dbModel.JournalTypeId) { case (int)JournalType.Enum.Expenses: break; case (int)JournalType.Enum.Recurring: ret = new RecurringJounralEntryModel() { Settlements = dbModel.RecurringExtension.Settlements, CycleDue = dbModel.RecurringExtension.CycleDue != null ? (decimal)dbModel.RecurringExtension.CycleDue : 0, FrequencyName = dbModel.RecurringExtension.FrequencyType.FrequencyName, FrequencyTypeId = dbModel.RecurringExtension.FrequencyType.FrequencyTypeId, InterestRate = dbModel.RecurringExtension.InterestRate, LoanDate = dbModel.RecurringExtension.LoanDate }; break; case (int)JournalType.Enum.MaintenanceParts: ret = new MaintenanceJournalEntryModel() { MaintenanceTypeId = dbModel.MaintenanceExtension.MaintenanceType.MaintenanceTypeId, MaintenanceTypeName = dbModel.MaintenanceExtension.MaintenanceType.MaintenanceTypeName, TransactionDate = dbModel.MaintenanceExtension.TransactionDate }; break; case (int)JournalType.Enum.Compliance: break; case (int)JournalType.Enum.Adjustment: break; default: throw new ArgumentException("Journal Type does not exist."); } ret.Amount = dbModel.Amount; ret.Description = dbModel.Description; if (dbModel.Driver != null) { // TODO: transfer driver info here. ret.Driver = new DriverModel() { DriverId = dbModel.DriverId, DriverFirstName = dbModel.Driver.FirstName, DriverLastName = dbModel.Driver.LastName, HireDate = dbModel.Driver.HireDate ?? new DateTime(2000, 1, 1), TermDate = dbModel.Driver.TermDate, DriverLicenseNumber = dbModel.Driver.DriverLicenseNumber }; } else { ret.Driver = new DriverModel(); } ret.DriverGLAccount = dbModel.DriverGLAccount; ret.JournalEntryId = dbModel.JournalEntryId; ret.Name = dbModel.JournalEntryName; ret.SMEGLAccount = dbModel.SMEGLAccount; ret.Type = dbModel.JournalType.JournalTypeName; ret.Contract = new ContractModel() { ContractId = dbModel.ContractId }; return(ret); }
/// <summary> /// Save a JournalEntryModel in a way specific to the type passed into the Journal Type Id. /// </summary> /// <param name="user">The user saving.</param> /// <param name="journalTypeId">The type of journal entry being saved.</param> /// <param name="model">The data to be saved to the database.</param> /// <returns></returns> public async static Task <JournalEntryModel> SaveJournalEntryModel(PortalUser user, int journalTypeId, JournalEntryModel model, IDriverRepository repository) { JournalEntryModel saveMe = model; JournalEntry dbEntry = null; switch (journalTypeId) { case (int)JournalType.Enum.Expenses: break; case (int)JournalType.Enum.Recurring: dbEntry = saveMe.JournalEntryId != 0 ? await repository.GetRecurringJournalEntryById(saveMe.JournalEntryId) : null; break; case (int)JournalType.Enum.MaintenanceParts: dbEntry = saveMe.JournalEntryId != 0 ? await repository.GetMaintenanceJournalEntryById(saveMe.JournalEntryId) : null; break; case (int)JournalType.Enum.Compliance: break; case (int)JournalType.Enum.Adjustment: break; default: throw new NullReferenceException("Journal Type does not exist."); } if (dbEntry != null && user.CompanyId != dbEntry.CompanyId) { throw new NullReferenceException("Journal Type does not exist."); } Driver driver = null; // Make sure the driver exists. if (saveMe.Driver == null && dbEntry == null) { throw new NullReferenceException("Driver is required to create."); } else if (saveMe.Driver != null) { driver = await repository.GetDriverById(saveMe.Driver.DriverId); if (driver == null && dbEntry == null) { throw new NullReferenceException($"Could not find driver with id {saveMe.Driver.DriverId}"); } } // Are we saving a new Journal or editing an old one? if (dbEntry == null && saveMe.JournalEntryId == 0) { dbEntry = new JournalEntry(); repository.Insert(dbEntry); dbEntry.CreatedBy = user.PortalUsername; dbEntry.CreatedDate = DateTime.UtcNow; } else if (dbEntry == null) { throw new NullReferenceException("Either Journal Entry Id or Jounral Type Id is incorrect."); } dbEntry.JournalEntryName = saveMe.Name; dbEntry.SMEGLAccount = saveMe.SMEGLAccount; // This can only be bypassed if the record already exists and we arent changin the driver. // Otherwise this if statement should be true. if (driver != null) { dbEntry.DriverId = driver.DriverId; } dbEntry.DriverGLAccount = saveMe.DriverGLAccount; dbEntry.Description = saveMe.Description; dbEntry.CompanyId = user.CompanyId; dbEntry.Amount = saveMe.Amount; dbEntry.JournalTypeId = journalTypeId; dbEntry.JournalType = await repository.GetJournalTypeById(dbEntry.JournalTypeId); if (dbEntry.JournalType == null) { throw new NullReferenceException("Could not find Journal Type."); } dbEntry.IsActive = true; // Save extension table as well. switch (journalTypeId) { case (int)JournalType.Enum.Expenses: break; case (int)JournalType.Enum.Recurring: // Casting for ease. RecurringJounralEntryModel recurring = (RecurringJounralEntryModel)saveMe; if (dbEntry.RecurringExtension == null) { dbEntry.RecurringExtension = new JournalRecurringExtension(); repository.Insert(dbEntry.RecurringExtension); dbEntry.RecurringExtension.CreatedBy = user.PortalUsername; dbEntry.RecurringExtension.CreatedDate = DateTime.UtcNow; } dbEntry.RecurringExtension.CycleDue = recurring.CycleDue; dbEntry.RecurringExtension.InterestRate = recurring.InterestRate; dbEntry.RecurringExtension.FrequencyTypeId = recurring.FrequencyTypeId; dbEntry.RecurringExtension.FrequencyType = await repository.GetFrequencyTypeById(recurring.FrequencyTypeId); if (dbEntry.RecurringExtension.FrequencyType == null) { throw new NullReferenceException("The frequency type does not exist."); } dbEntry.RecurringExtension.LoanDate = recurring.LoanDate; dbEntry.RecurringExtension.Settlements = recurring.Settlements; dbEntry.RecurringExtension.IsActive = true; if (repository.IsChanged(dbEntry.RecurringExtension)) { dbEntry.RecurringExtension.ModifiedBy = user.PortalUsername; dbEntry.RecurringExtension.ModifiedDate = DateTime.UtcNow; } break; case (int)JournalType.Enum.MaintenanceParts: // Casting for ease. MaintenanceJournalEntryModel maintenance = (MaintenanceJournalEntryModel)saveMe; if (dbEntry.MaintenanceExtension == null) { dbEntry.MaintenanceExtension = new JournalMaintenanceExtension(); repository.Insert(dbEntry.MaintenanceExtension); dbEntry.MaintenanceExtension.CreatedBy = user.PortalUsername; dbEntry.MaintenanceExtension.CreatedDate = DateTime.UtcNow; } dbEntry.MaintenanceExtension.IsActive = true; dbEntry.MaintenanceExtension.TransactionDate = maintenance.TransactionDate; dbEntry.MaintenanceExtension.MaintenanceTypeId = maintenance.MaintenanceTypeId; dbEntry.MaintenanceExtension.MaintenanceType = await repository.GetMaintenanceById(maintenance.MaintenanceTypeId); if (dbEntry.MaintenanceExtension.MaintenanceType == null) { throw new NullReferenceException("The maintenance type does not exist."); } if (repository.IsChanged(dbEntry.MaintenanceExtension)) { dbEntry.MaintenanceExtension.ModifiedBy = user.PortalUsername; dbEntry.MaintenanceExtension.ModifiedDate = DateTime.UtcNow; } break; case (int)JournalType.Enum.Compliance: break; case (int)JournalType.Enum.Adjustment: break; default: break; } if (repository.IsChanged(dbEntry)) { dbEntry.ModifiedBy = user.PortalUsername; dbEntry.ModifiedDate = DateTime.UtcNow; } await repository.SaveChanges(); return(LoadJournalEntry(dbEntry)); }
public async void Post_XmlJournalEntryModelObject_ResponseIsSuccess() { var journalEntryModel = new JournalEntryModel() { Distance = 3000, Duration = new TimeSpan(1, 25, 56), Time = new DateTimeOffset(DateTime.Today), // Born = DateTime.Today, }; string json = JsonConvert.SerializeObject(journalEntryModel); //arrange using (var client = TestHttpClientFactory.Create("JournalController")) { //act // var response = client.PostAsJsonAsync().Result; client.DefaultRequestHeaders.Accept.ParseAdd("application/xml"); var response = await client.PostAsJsonAsync("", json); var returnPerson = await response.Content.ReadAsAsync<JournalEntryModel>(); //assert Assert.That(response.IsSuccessStatusCode, Is.True, "response:" + response); //Assert.That(returnPerson.Id, Is.GreaterThan(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)); } } }
public void PostWithoutUserNameReturnsCorrectResponse() { var projectionStub = new Mock<IUserNameProjection>(); var queryDummy = new Mock<IJournalEntriesQuery>(); var cmdMock = new Mock<IAddJournalEntryCommand>(); var sut = new JournalController( projectionStub.Object, queryDummy.Object, cmdMock.Object) { Request = new HttpRequestMessage() }; sut.Request.Properties.Add( HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration()); projectionStub .Setup(p => p.GetUserName(It.IsAny<HttpRequestMessage>())) .Returns((string)null); var dummyEntry = new JournalEntryModel(); var response = sut.Post(dummyEntry); Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode); cmdMock.Verify( c => c.AddJournalEntry(It.IsAny<JournalEntryModel>(), It.IsAny<string>()), Times.Never); }
public void PostInsertsEntry(string userName) { var projectionStub = new Mock<IUserNameProjection>(); var queryDummy = new Mock<IJournalEntriesQuery>(); var cmdMock = new Mock<IAddJournalEntryCommand>(); var sut = new JournalController( projectionStub.Object, queryDummy.Object, cmdMock.Object) { Request = new HttpRequestMessage() }; sut.Request.Properties.Add( HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration()); projectionStub.Setup(p => p.GetUserName(sut.Request)).Returns(userName); var entry = new JournalEntryModel(); sut.Post(entry); cmdMock.Verify(c => c.AddJournalEntry(entry, userName)); }
public void SaveTrainingLogDetails(JournalEntryModel saveModel) { }
/// <summary> /// Creates a journal entry to the database. /// </summary> /// <param name="journalEntry">A model that contains the journal entry info /// to be created.</param> public void Create(JournalEntryModel journalEntry) { }
public IHttpActionResult SearchJournal(string searchValue) { var upperSearchValue = searchValue.ToUpper(); var retVal = new List <JournalEntryModel>(); try { using (var db = new DiabetesFoodJournalContext()) { var results = db.JournalEntries.AsNoTracking().Where(entry => entry.Title.ToUpper().Contains(upperSearchValue)) .Include(x => x.JournalEntryNutritionalInfos.Select(y => y.NutritionalInfo)) .Include(x => x.JournalEntryDoses.Select(y => y.Dose)) .Include(x => x.JournalEntryTags.Select(y => y.Tag)); if (results.Any()) { foreach (var result in results) { var currentEntry = new JournalEntryModel(); currentEntry.Tags = new List <TagModel>(); currentEntry.Id = result.Id; currentEntry.Logged = result.Logged; currentEntry.Notes = result.Notes; currentEntry.Title = result.Title; if (result.JournalEntryNutritionalInfos != null && result.JournalEntryNutritionalInfos.Any()) { var nutrition = result.JournalEntryNutritionalInfos.FirstOrDefault().NutritionalInfo; if (nutrition.Id > 0) { currentEntry.NutritionalInfo = new NutritionalInfoModel(); currentEntry.NutritionalInfo.Id = nutrition.Id; currentEntry.NutritionalInfo.Calories = nutrition.Calories; currentEntry.NutritionalInfo.Carbohydrates = nutrition.Carbohydrates; currentEntry.NutritionalInfo.Protein = nutrition.Protein; } } if (result.JournalEntryDoses != null && result.JournalEntryDoses.Any()) { var dose = result.JournalEntryDoses.FirstOrDefault().Dose; if (dose.Id > 0) { currentEntry.Dose = new DoseModel(); currentEntry.Dose.Id = dose.Id; currentEntry.Dose.Extended = dose.Extended; currentEntry.Dose.InsulinAmount = dose.InsulinAmount; currentEntry.Dose.TimeExtended = dose.TimeExtended; currentEntry.Dose.TimeOffset = dose.TimeOffset; currentEntry.Dose.UpFront = dose.UpFront; } } if (result.JournalEntryTags != null && result.JournalEntryTags.Any()) { foreach (var tag in result.JournalEntryTags) { var tagViewModel = new TagModel(); tagViewModel.Id = tag.Tag.Id; tagViewModel.Description = tag.Tag.Description; currentEntry.Tags.Add(tagViewModel); } } retVal.Add(currentEntry); } } } } catch (Exception e) { return(BadRequest(e.Message)); } return(Ok(retVal.Take(100))); }