public MealEntity Convert(Meal source, MealEntity destination, ResolutionContext context)
        {
            using (var dbContext = _factory.Create())
            {
                destination = new MealEntity
                {
                    Id    = source.Id,
                    Price = source.Price
                };
                destination.Id = source.Id;
                destination.FoodEntityMealEntities = new List <FoodEntityMealEntities>();
                foreach (var food in source.Foods)
                {
                    var dbFoodMealEntity = dbContext.Meal.FirstOrDefault(m => m.Id == source.Id).FoodEntityMealEntities.SingleOrDefault(fm => fm.FoodEntityId == food.Id);
                    if (dbFoodMealEntity == null)
                    {
                        destination.FoodEntityMealEntities.Add(new FoodEntityMealEntities
                        {
                            FoodEntity   = _mapper.Map <FoodEntity>(food),
                            FoodEntityId = food.Id,
                            MealEntityId = source.Id,
                            MealEntity   = destination
                        });
                    }
                    else
                    {
                        destination.FoodEntityMealEntities.Add(dbFoodMealEntity);
                    }
                }

                return(destination);
            }
        }
Example #2
0
        /// <inheritdoc />
        public async Task DeleteAsync(int id)
        {
            MealEntity mealEntity = await _mealsRepository.GetByIdAsync(id);

            if (mealEntity == null)
            {
                throw new NotFoundException();
            }

            await _mealsRepository.DeleteAsync(mealEntity);
        }
Example #3
0
        /// <inheritdoc />
        public async Task <MealDto> GetByIdAsync(int id)
        {
            MealEntity mealEntity = await _mealsRepository.GetByIdAsync(id);

            if (mealEntity == null)
            {
                throw new NotFoundException();
            }

            var result = _mapper.Map <MealDto>(mealEntity);

            return(result);
        }
Example #4
0
        public async Task InsertMealIntoTable(MealModel model)
        {
            var table = await _tableStorage.GetTableReference(_mealsTable);

            var entity = new MealEntity()
            {
                PartitionKey   = Guid.NewGuid().ToString(),
                RowKey         = new Guid().ToString(),
                MealsModelData = model
            };

            var tableOperation = TableOperation.InsertOrMerge(entity);
            await table.ExecuteAsync(tableOperation);
        }
        public static MealModel Map(MealEntity entity)
        {
            if (entity == null)
            {
                return(null);
            }

            return(new MealModel()
            {
                Id = entity.Id,
                Name = entity.Name,
                Description = entity.Description,
                Price = entity.Price,
                Weight = entity.Weight,
                Preview = ImageMapper.Map(entity.Preview),
            });
        }
Example #6
0
        /// <inheritdoc />
        public async Task UpdateAsync(MealDto mealDto)
        {
            MealEntity mealEntity = null;

            if (mealDto.Id != 0)
            {
                mealEntity = await _mealsRepository.GetByIdAsync(mealDto.Id);
            }

            if (mealEntity == null)
            {
                throw new NotFoundException();
            }

            _mapper.Map(mealDto, mealEntity);
            await _mealsRepository.UpdateAsync(mealEntity);
        }
Example #7
0
 public Either <Error, int> Insert(MealInsertModel entity)
 {
     try
     {
         using (var context = _factory.Create())
         {
             var mealEntity = new MealEntity
             {
                 Price = entity.Price,
                 FoodEntityMealEntities = entity.Foods.Select(x => new FoodEntityMealEntities {
                     FoodEntityId = x
                 }).ToList(),
             };
             var addedEntity = context.Add(mealEntity).Entity;
             context.SaveChanges();
             return(new Right <Error, int>(addedEntity.Id));
         }
     }
     catch (Exception ex)
     {
         return(new Left <Error, int>(new UnknownError(ex.ToString())));
     }
 }
Example #8
0
 /// <inheritdoc />
 public async Task DeleteAsync(MealEntity meal)
 {
     _dbContext.Remove(meal);
     await _dbContext.SaveChangesAsync();
 }
Example #9
0
 /// <inheritdoc />
 public async Task UpdateAsync(MealEntity meal)
 {
     meal.UserId = _userHelper.UserId;
     await _dbContext.SaveChangesAsync();
 }
Example #10
0
 /// <inheritdoc />
 public async Task CreateAsync(MealEntity meal)
 {
     meal.UserId = _userHelper.UserId;
     _dbContext.Meals.Add(meal);
     await _dbContext.SaveChangesAsync();
 }