示例#1
0
        public IActionResult Post(DayMealDTO dto)
        {
            try
            {
                DayMeal = ApiRepository.DtoToDayMeal(dto);

                var exists = dayMealData.GetById(dto.DayMealId);

                if (exists != null)
                {
                    return(BadRequest("Cannot add, entity already exists!"));
                }

                DayMeal = dayMealData.SetValuesByIds(dto.DayId, dto.MealId, dto.Category);

                dayMealData.Add(DayMeal);

                if (dayMealData.Commit() > 0)
                {
                    return(CreatedAtAction("GetById", new { id = DayMeal.DayMealId }, DayMeal));
                }
            }
            catch (Exception)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Database Failure"));
            }

            return(BadRequest());
        }
示例#2
0
        public static List <DayMealDTO> DayMealsToDto(List <DayMeal> meals)
        {
            var result = new List <DayMealDTO>();
            var dto    = new DayMealDTO();

            foreach (var m in meals)
            {
                dto = DayMealToDto(m);
                result.Add(dto);
            }
            return(result);
        }
示例#3
0
 public static DayMeal DtoToDayMeal(DayMealDTO product, DayMeal ent = null)
 {
     if (product != null)
     {
         return(ent = ent ?? new DayMeal
         {
             DayMealId = product.DayMealId,
             DayId = product.DayId,
             MealId = product.MealId,
             Category = product.Category
         });
     }
     return(null);
 }
示例#4
0
 public static DayMealDTO DayMealToDto(DayMeal product, DayMealDTO dto = null)
 {
     if (product != null)
     {
         return(dto = dto ?? new DayMealDTO
         {
             DayMealId = product.DayMealId,
             DayId = product.DayId,
             MealId = product.MealId,
             Category = product.Category
         });
     }
     return(null);
 }
示例#5
0
        public IActionResult Put(int id, DayMealDTO dto)
        {
            try
            {
                DayMeal = dayMealData.GetById(id);
                if (DayMeal == null)
                {
                    return(NotFound($"Could not find entity with id{id}!"));
                }

                ApiRepository.DtoToDayMeal(dto, DayMeal);
                dayMealData.Update(DayMeal);

                if (dayMealData.Commit() > 0)
                {
                    return(Ok("Updated!"));
                }
            }
            catch (Exception)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Database Failure"));
            }
            return(BadRequest());
        }