public async Task <IActionResult> Delete([FromBody] WeightLogBindingModel model) { var user = await _userManager.GetUserAsync(User); var log = _dbContext.WeightLogs.Find(model.Id); if (log.UserId != user.Id) { return(Unauthorized("You are not authorized to delete this log")); } _dbContext.WeightLogs.Remove(log); await _dbContext.SaveChangesAsync(); return(Ok()); }
public async Task <IActionResult> Add([FromBody] WeightLogBindingModel model) { var user = await _userManager.GetUserAsync(User); var existingLogForToday = await _dbContext.WeightLogs.AnyAsync(l => l.LoggedOn.Date == DateTime.UtcNow.Date && l.UserId == user.Id); if (existingLogForToday) { return(BadRequest("You already log your weight today")); } var log = new WeightLog() { LoggedOn = DateTime.UtcNow, UserId = user.Id, Weight = model.Weight }; _dbContext.WeightLogs.Add(log); await _dbContext.SaveChangesAsync(); return(Ok()); }