public async Task <IActionResult> UpsertCalorieEntry(CalorieEntryDTO dto) { try { var calorieEntry = _dbContext.CalorieEntries.Where(q => q.Id == dto.Id).FirstOrDefault(); if (calorieEntry == null) { calorieEntry = new CalorieEntry(); _dbContext.CalorieEntries.Add(calorieEntry); calorieEntry.CreatedAt = DateTime.Now; calorieEntry.UserId = 1; } calorieEntry.Quantity = dto.Quantity > 0 == false ? 1 : dto.Quantity; calorieEntry.Calories = dto.Calories; calorieEntry.Carbs = dto.Carbs; calorieEntry.EntryName = dto.EntryName; calorieEntry.Protein = dto.Protein; calorieEntry.Fat = dto.Fat; await _dbContext.SaveChangesAsync(); dto.Id = calorieEntry.Id; return(Ok(dto)); } catch (Exception ex) { return(BadRequest(AppConstants.GenericErrorMsg)); } }
public async Task <ActionResponse <CalorieEntryDTO> > SaveAsync(CalorieEntryDTO dto) { var response = new ActionResponse <CalorieEntryDTO>(); var entry = _dbContext.CalorieEntries.Where(q => q.Id == dto.Id).FirstOrDefault(); if (entry == null) { entry = new CalorieEntry(); entry.CreatedAt = DateTime.Now; } entry.EntryName = dto.EntryName; entry.Calories = dto.Calories; entry.Carbs = dto.Carbs; entry.Fat = dto.Fat; entry.Protein = dto.Protein; await _dbContext.SaveChangesAsync(); dto.Id = entry.Id; response.ResultObj = dto; return(response); }
private CalorieEntryDTO MapToCalorieEntryDTO(CalorieEntry entry) { var dto = new CalorieEntryDTO { Id = entry.Id, UserId = entry.UserId, Calories = entry.Calories, Carbs = entry.Carbs, Protein = entry.Protein, Fat = entry.Fat, CreatedAt = entry.CreatedAt, EntryName = entry.EntryName, Quantity = entry.Quantity }; return(dto); }