public async Task <IActionResult> Edit(Guid id, [Bind("Id,UserName,TotalCarbs,CreatedAt,UpdatedAt,Timestamp")] MealEntry mealEntry) { if (id != mealEntry.Id) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(mealEntry); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!MealEntryExists(mealEntry.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } ViewData["UserName"] = new SelectList(_context.Patients, "Id", "Id", mealEntry.UserName); return(View(mealEntry)); }
public List <DAO.MealEntry> getAllMealEntryInfo() { try { aSqlConnection.Open(); SqlCommand command = new SqlCommand("USPMealEntries", aSqlConnection); command.CommandType = CommandType.StoredProcedure; SqlDataReader aReader = command.ExecuteReader(); aMealEntryList = new List <MealEntry>(); if (aReader.HasRows) { while (aReader.Read()) { MealEntry aMealEntry = new MealEntry(); aMealEntry.memberId = (int)aReader[0]; aMealEntry.noOfMeals = Convert.ToInt32(aReader[1].ToString()); aMealEntry.mealEntryDate = (DateTime)aReader[2]; aMealEntryList.Add(aMealEntry); } } } catch (Exception e) { MessageBox.Show(e.Message); } finally { if (aSqlConnection != null && aSqlConnection.State == ConnectionState.Open) { aSqlConnection.Close(); } } return(aMealEntryList); }
public async Task <IActionResult> Edit(int id, [Bind("MealEntryId,MealTime,Calories,PetId")] MealEntry mealEntryToUpdate, int page = 1) { if (User.Identity.IsAuthenticated) { if (id != mealEntryToUpdate.MealEntryId) { return(NotFound()); } if (ModelState.IsValid) { var updateStatus = await _mealEntryService.UpdateMealEntryAsync(mealEntryToUpdate); if (!updateStatus) { return(RedirectToAction("Index", "EditEntries", new { error = true, msgToDisplay = "An error has occured with the databse." })); } var goalReached = _mealEntryService.DailyMealGoalReached(mealEntryToUpdate.PetId); return(RedirectToAction("Index", "EditEntries", new { id = page, updated = true, msgToDisplay = "Meal entry has been successfully updated." })); } ViewBag.Page = page; return(View(mealEntryToUpdate)); } return(RedirectToAction("Index", "NotAuthorized")); }
} // DeleteAsync public async Task CreateOrUpdateEntries( ICollection<MealEntry> mealEntries ) { foreach ( MealEntry mealEntry in mealEntries ) { MealEntry dbMealEntry = await ReadAsync( mealEntry.Id ); if ( dbMealEntry == null ) // If meal entry doesn't exist { // Create in the database await CreateAsync( mealEntry ); } else if ( dbMealEntry.UpdatedAt < mealEntry.UpdatedAt ) { // Update in the database await UpdateAsync( mealEntry.Id, mealEntry ); } // Check whether meal items need to be created/updated: await _mealItemRepository.CreateOrUpdateEntries( mealEntry.MealItems ); } // foreach MealEntry return; } // CreateOrUpdateEntries
} // Create public async Task UpdateAsync( Guid id, MealEntry mealEntry ) { var oldMealEntry = await ReadAsync( id ); if( oldMealEntry != null ) { oldMealEntry.UserName = mealEntry.UserName; oldMealEntry.Patient = mealEntry.Patient; oldMealEntry.TotalCarbs = mealEntry.TotalCarbs; oldMealEntry.CreatedAt = mealEntry.CreatedAt; oldMealEntry.UpdatedAt = mealEntry.UpdatedAt; oldMealEntry.Timestamp = mealEntry.Timestamp; //oldMealEntry.MealItems = mealEntry.MealItems; _db.Entry( mealEntry.MealItems ).State = EntityState.Unchanged; _db.Entry( oldMealEntry.MealItems ).State = EntityState.Unchanged; foreach( var mealItem in mealEntry.MealItems ) _db.Entry( mealItem ).State = EntityState.Unchanged; foreach ( var mealItem in oldMealEntry.MealItems ) _db.Entry( mealItem ).State = EntityState.Unchanged; _db.Entry( oldMealEntry ).State = EntityState.Modified; await _db.SaveChangesAsync(); return; } } // UpdateAsync
public async Task <bool> UpdateMealEntryAsync(MealEntry mealEntryToUpdate) { _dbContext.MealEntries.Update(mealEntryToUpdate); var updated = await _dbContext.SaveChangesAsync(); return(updated > 0); }
} // ReadAll public async Task<MealEntry> CreateAsync( MealEntry mealentry ) { _db.MealEntries.Add( mealentry ); await _db.SaveChangesAsync(); return mealentry; } // Create
public async Task <bool> CreateMealEntryAsync(MealEntry mealEntry) { await _dbContext.MealEntries.AddAsync(mealEntry); var created = await _dbContext.SaveChangesAsync(); return(created > 0); }
public async Task <RecordMealResponse> RecordMeal(MealEntry mealEntry) { mealEntry.Date = DateTime.UtcNow; await mealEntryRepository.Add(mealEntry); await unitOfWork.Complete(); return(new RecordMealResponse(true, null)); }
public async Task <bool> CreateMealEnty(int calories, int petId) { var mealEntry = new MealEntry { Calories = calories, MealTime = DateTime.Now, PetId = petId }; return(await _mealEntryService.CreateMealEntryAsync(mealEntry)); }
public MealEntryViewModel MapMealEntry(MealEntry mealEntry) { var mealEntryViewModel = new MealEntryViewModel { PetId = mealEntry.PetId, MealEntryId = mealEntry.MealEntryId, Calories = mealEntry.Calories, MealTime = mealEntry.MealTime }; return(mealEntryViewModel); }
public void SmokeTest() { var entry = new MealEntry ( dishes: new string[] { "one", "two"}, allowedMultiple: new int[] { 1} ); entry.Should().NotBeNull(); entry.Dishes.Count().Should().Be(2); entry.AllowedMultiple.Count().Should().Be(1); }
public async Task <IActionResult> Create([Bind("Id,UserName,TotalCarbs,CreatedAt,UpdatedAt,Timestamp")] MealEntry mealEntry) { if (ModelState.IsValid) { mealEntry.Id = Guid.NewGuid(); _context.Add(mealEntry); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["UserName"] = new SelectList(_context.Patients, "Id", "Id", mealEntry.UserName); return(View(mealEntry)); }
private void saveButton_Click(object sender, System.EventArgs e) { try { MealEntry aMealEntry = new MealEntry(Convert.ToInt32(memberIdComboBox.Text), Convert.ToInt32(noOfMealsComboBox.Text), dateTimePicker1.Value); string msg = aMealEntryBll.save(aMealEntry); MessageBox.Show(msg, @"Message"); showDataInDataGridView(); clearTextBoxes(); } catch (Exception exception) { MessageBox.Show(exception.Message); } }
} // ReadAll public async Task<MealEntry> CreateAsync( MealEntry mealentry ) { _db.MealEntries.Add( mealentry ); await _db.SaveChangesAsync(); if( Config.AuditingOn ) { var auditChange = new AuditChange(); auditChange.CreateAuditTrail( AuditActionType.CREATE, mealentry.Id.ToString(), new MealEntry(), mealentry ); await _auditRepo.CreateAsync( auditChange ); } // if return mealentry; } // Create
}// End CreateMealItem[Post]. public async Task<IActionResult> MealEntryUpdate( string userName, Guid loginToken, Guid mealId ) { if ( ModelState.IsValid ) { ApplicationUser user = await _users.ReadAsync(userName);// Read user from the repository user.RemoteLoginToken = Guid.NewGuid(); // Create a login token, similar to a "session id" MealEntry meal = await _meal.ReadAsync(mealId); var mealModel = new MealEntryViewModel { UserName = meal.UserName, Patient = meal.Patient, TotalCarbs = meal.TotalCarbs, Date = meal.CreatedAt, Timestamp = meal.Timestamp, MealItems = meal.MealItems, }; await _meal.UpdateAsync( meal.Id, mealModel.GetNewMealEntry() ); return new JsonResult( // This implements IActionResult. If you were new // to inspect the output, you would see a { // Json-formatted string. success = true, errorCode = ErrorCode.NO_ERROR, remoteMealToken = _meal.ToString(), meal.UserName, meal.Patient, meal.TotalCarbs, meal.CreatedAt, meal.Timestamp, meal.MealItems } ); }//end if(ModelState.IsValid) return new JsonResult( new { success = false, errorCode = ErrorCode.UNKNOWN } ); }//end MealEntrySync
public async Task <IActionResult> Create([Bind("MealEntryId,MealTime,Calories,PetId")] MealEntry newMealEntry) { if (User.Identity.IsAuthenticated) { if (ModelState.IsValid) { newMealEntry.MealTime = DateTime.Now; var returnValue = await _mealEntryService.CreateMealEntryAsync(newMealEntry); if (!returnValue) { return(RedirectToAction("Index", "Home", new { error = true, msgToDisplay = "An error has occured with the database" })); } var goalReached = _mealEntryService.DailyMealGoalReached(newMealEntry.PetId); return(RedirectToAction("Index", "Home", new { goalReached = goalReached, goalText = "daily calorie goal", added = true, msgToDisplay = "New meal entry has been addded!" })); } var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); ViewBag.NoPets = false; ViewData["PetId"] = await _petService.GetPetsSelectListAsync(userId); return(View(newMealEntry)); } return(RedirectToAction("Index", "NotAuthorized")); }
} // Create public async Task UpdateAsync( Guid id, MealEntry mealEntry ) { var dbMealEntry = await ReadAsync( id ); if( dbMealEntry != null ) { if( Config.AuditingOn ) { var auditChange = new AuditChange(); auditChange.CreateAuditTrail( AuditActionType.UPDATE, mealEntry.Id.ToString(), dbMealEntry, mealEntry ); await _auditRepo.CreateAsync( auditChange ); } // if dbMealEntry.UserName = mealEntry.UserName; dbMealEntry.Patient = mealEntry.Patient; dbMealEntry.TotalCarbs = mealEntry.TotalCarbs; dbMealEntry.CreatedAt = mealEntry.CreatedAt; dbMealEntry.UpdatedAt = mealEntry.UpdatedAt; dbMealEntry.Timestamp = mealEntry.Timestamp; //oldMealEntry.MealItems = mealEntry.MealItems; _db.Entry( mealEntry.MealItems ).State = EntityState.Unchanged; _db.Entry( dbMealEntry.MealItems ).State = EntityState.Unchanged; foreach( var mealItem in mealEntry.MealItems ) _db.Entry( mealItem ).State = EntityState.Unchanged; foreach( var mealItem in dbMealEntry.MealItems ) _db.Entry( mealItem ).State = EntityState.Unchanged; _db.Entry( dbMealEntry ).State = EntityState.Modified; await _db.SaveChangesAsync(); return; } } // UpdateAsync
public async Task Add(MealEntry mealEntry) { await context.AddAsync(mealEntry); }
public async Task<IActionResult> CreateMealEntry( string userName, Guid loginToken, MealEntry mealEntry ) { // Get user from username, verify login token var user = await _users.ReadAsync( userName ); if( user.RemoteLoginToken != loginToken ) // Check login token { return new JsonResult( // Return error new { success = false, errorCode = ErrorCode.INVALID_LOGIN_TOKEN } ); } // if if ( !_meal.ReadAll().Any( o => o.Id == mealEntry.Id ) )// Ensure meal doesn't exist first { return new JsonResult( // If it does, return error new { success = false, errorCode = ErrorCode.ITEM_ALREADY_EXISTS } ); } if ( ModelState.IsValid ) { var newMealEntry = await _meal.CreateAsync( mealEntry ); return new JsonResult( // Return success new { success = true, errorCode = ErrorCode.NO_ERROR, newMealEntry.Id } ); } // if return new JsonResult( // Return unknown error new { success = false, errorCode = ErrorCode.UNKNOWN } ); } // CreateMealEntry