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);
        }
        public ActionResult DeleteConfirmed(int id)
        {
            CalorieEntry calorieEntry = db.CalorieEntries.Find(id);

            db.CalorieEntries.Remove(calorieEntry);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
Ejemplo n.º 4
0
        public async Task <ServiceResult> UpdateEntryAsync(CalorieEntry entry)
        {
            await _entriesCollection.ReplaceOneAsync(q => q.Id == entry.Id && q.UserId == entry.UserId, entry);

            _serviceResult.ResultCollection.Add(MapToCalorieEntryDTO(entry));

            return(_serviceResult);
        }
Ejemplo n.º 5
0
        private bool ValidateEntry(CalorieEntry entry)
        {
            if (string.IsNullOrEmpty(entry.UserId))
            {
                _serviceResult.Errors.Add("UserId is required.");
            }

            return(_serviceResult.Errors.Any());
        }
 public ActionResult Edit([Bind(Include = "ID,Calories,DateAdded,Quality")] CalorieEntry calorieEntry)
 {
     if (ModelState.IsValid)
     {
         db.Entry(calorieEntry).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(calorieEntry));
 }
Ejemplo n.º 7
0
        public async Task <ServiceResult> AddEntryAsync(CalorieEntry entry)
        {
            if (ValidateEntry(entry) == false)
            {
                return(_serviceResult);
            }

            await _entriesCollection.InsertOneAsync(entry);

            return(null);
        }
        public ActionResult Create([Bind(Include = "ID,Calories,DateAdded,Quality")] CalorieEntry calorieEntry)
        {
            if (ModelState.IsValid)
            {
                calorieEntry.DateAdded = DateTime.UtcNow;
                db.CalorieEntries.Add(calorieEntry);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(calorieEntry));
        }
        // GET: CalorieEntries/Delete/5
        public ActionResult Delete(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            CalorieEntry calorieEntry = db.CalorieEntries.Find(id);

            if (calorieEntry == null)
            {
                return(HttpNotFound());
            }
            return(View(calorieEntry));
        }
Ejemplo n.º 10
0
        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);
        }
Ejemplo n.º 11
0
        private CalorieEntry MapToCalorieEntry(CalorieEntryUpsertBindingModel model)
        {
            var entry = new CalorieEntry
            {
                Id        = model.Id,
                UserId    = model.UserId,
                Calories  = model.Calories,
                Fat       = model.Fat,
                Carbs     = model.Carbs,
                Protein   = model.Protein,
                Quantity  = model.Quantity,
                EntryName = model.EntryName,
                CreatedAt = model.CreatedAt
            };

            return(entry);
        }