Esempio n. 1
0
        public async Task LearnRight(Food food, string idParent)
        {
            if (food.Name == null)
            {
                throw new NullReferenceException("A Name must be provided");
            }

            IndexNearestName(food);
            _foodRepository.Add(food);

            if (idParent != null)
            {
                _foodRepository.UpdateNextRight(idParent, food.Id);
            }
        }
Esempio n. 2
0
        public IActionResult AddFood([FromBody] FoodItemCreateDto foodItemViewModel)
        {
            if (foodItemViewModel == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            FoodItem toAdd = Mapper.Map <FoodItem>(foodItemViewModel);

            _foodRepository.Add(toAdd);

            if (!_foodRepository.Save())
            {
                throw new Exception("Creating a fooditem failed on save.");
            }

            FoodItem newFoodItem = _foodRepository.GetSingle(toAdd.Id);

            _hubContext.Clients.All.SendAsync("food-added", Mapper.Map <FoodItemDto>(newFoodItem));
            return(CreatedAtRoute(nameof(GetSingleFood), new { id = newFoodItem.Id },
                                  Mapper.Map <FoodItemDto>(newFoodItem)));
        }
        public IActionResult Add([FromBody] FoodItemViewModel foodItemViewModel)
        {
            if (foodItemViewModel == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            FoodItem toAdd = Mapper.Map <FoodItem>(foodItemViewModel);

            _foodRepository.Add(toAdd);

            if (!_foodRepository.Save())
            {
                throw new Exception("Creating a fooditem failed on save.");
            }

            FoodItem newFoodItem = _foodRepository.GetSingle(toAdd.Id);

            return(CreatedAtRoute("GetSingleFood", new { id = newFoodItem.Id },
                                  Mapper.Map <FoodItemViewModel>(newFoodItem)));
        }
        public ActionResult <FoodItemDto> AddFood([FromBody] FoodCreateDto foodCreateDto)
        {
            if (foodCreateDto == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            FoodItem toAdd = Mapper.Map <FoodItem>(foodCreateDto);

            _foodRepository.Add(toAdd);

            if (!_foodRepository.Save())
            {
                throw new Exception("Creating a fooditem failed on save.");
            }

            FoodItem newFoodItem = _foodRepository.GetSingle(toAdd.Id);

            return(CreatedAtRoute(nameof(GetSingleFood), new { id = newFoodItem.Id },
                                  Mapper.Map <FoodItemDto>(newFoodItem)));
        }
Esempio n. 5
0
        public FoodDto AddFood(FoodDto dto)
        {
            Food    food           = _modelMapper.FromFoodDto(dto);
            Food    createdFood    = _foodRepository.Add(food);
            FoodDto createdFoodDto = _modelMapper.ToFoodDto(createdFood);

            return(createdFoodDto);
        }
Esempio n. 6
0
 public void EnsureSeedData()
 {
     _repository.Add(new FoodItem()
     {
         Calories = 72, Id = 1, Name = "Apple", Created = DateTime.Now
     });
     _repository.Add(new FoodItem()
     {
         Calories = 105, Id = 2, Name = "Banana", Created = DateTime.Now
     });
     _repository.Add(new FoodItem()
     {
         Calories = 102, Id = 3, Name = "Egg", Created = DateTime.Now
     });
     _repository.Add(new FoodItem()
     {
         Calories = 221, Id = 4, Name = "Spaghetti", Created = DateTime.Now
     });
 }
Esempio n. 7
0
 public void EnsureSeedData()
 {
     _repository.Add(new FoodItem()
     {
         Calories = 1000, Id = 1, Name = "Lasagne", Created = DateTime.Now
     });
     _repository.Add(new FoodItem()
     {
         Calories = 1100, Id = 2, Name = "Hamburger", Created = DateTime.Now
     });
     _repository.Add(new FoodItem()
     {
         Calories = 1200, Id = 3, Name = "Spaghetti", Created = DateTime.Now
     });
     _repository.Add(new FoodItem()
     {
         Calories = 1300, Id = 4, Name = "Pizza", Created = DateTime.Now
     });
 }
Esempio n. 8
0
        public IResult Add(Foods food)
        {
            var result = CheckFoodNameExists(food.Name);

            if (!result.IsSuccess)
            {
                return(result);
            }
            _foodRepository.Add(food);
            return(new SuccessResult(Messages.FoodAdded));
        }
 private static void AddTestData(IFoodRepository repository)
 {
     for (int i = 0; i < 5; i++)
     {
         FoodItem foodItem = new FoodItem();
         foodItem.Created  = DateTime.Now;
         foodItem.Calories = 999;
         foodItem.Name     = Guid.NewGuid().ToString();
         foodItem.Type     = "Starter";
         repository.Add(foodItem);
     }
 }
        public IActionResult Add([FromBody] FoodItem foodItem)
        {
            try
            {
                FoodItem newFoodItem = _foodRepository.Add(foodItem);

                return(Ok(Mapper.Map <FoodItemViewModel>(newFoodItem)));
            }
            catch (Exception exception)
            {
                return(new StatusCodeResult(500));
            }
        }
Esempio n. 11
0
        public IHttpActionResult Add(FoodItem foodItem)
        {
            try
            {
                FoodItem newFoodItem = _foodRepository.Add(foodItem);

                return(Ok(Mapper.Map <FoodItemViewModel>(newFoodItem)));
            }
            catch (Exception exception)
            {
                return(InternalServerError(exception));
            }
        }
Esempio n. 12
0
        public async Task <IActionResult> Create([FromBody] FoodDTO dto, CancellationToken cancellationToken = default)
        {
            var store = await _storeRepository.FindByIdAsync(dto.StoreId);

            if (store == null)
            {
                return(NotFound("No Store Found"));
            }

            var food = _mapper.Map <Food>(dto);

            _foodRepository.Add(food);
            await _foodRepository.SaveChangesAsync(cancellationToken);

            return(CreatedAtAction(nameof(GetById), new { food.Id }, _mapper.Map <FoodDTO>(food)));
        }
Esempio n. 13
0
        public IActionResult Add([FromBody] FoodItemViewModel foodItemViewModel)
        {
            if (foodItemViewModel == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            FoodItem newFoodItem = _foodRepository.Add(Mapper.Map <FoodItem>(foodItemViewModel));

            return(CreatedAtRoute("GetSingleFood", new { id = newFoodItem.Id }, Mapper.Map <FoodItemViewModel>(newFoodItem)));
        }
        public IActionResult AddNewFoodItem([FromBody] FoodDto FoodDto)
        {
            if (FoodDto == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            FoodItem created = foodRepository.Add(AutoMapper.Mapper.Map <FoodItem>(FoodDto));

            return(CreatedAtRoute("GetSingleFoodItem", new { id = created.Id }, AutoMapper.Mapper.Map <FoodDto>(created)));
        }
Esempio n. 15
0
        public async Task <BaseResult <FoodEntity> > Add([FromForm] FoodEntity entity)
        {
            if (entity.image == null)
            {
                IFormFileCollection file = Request.Form.Files;
                if (file != null && file.Count == 1)
                {
                    //文件存放路径
                    string path = "";
                    //path = $"{_hostingEnvironment.WebRootPath}/Upload/Food";
                    //path = "E:/Upload/Food";
                    path = "C:/backTh/wwwroot/Food";// 绝对路劲
                    if (!Directory.Exists(path))
                    {
                        //路径不存在则新建路径
                        if (!string.IsNullOrEmpty(path))
                        {
                            Directory.CreateDirectory(path);
                        }
                    }
                    string fileName    = file[0].FileName;
                    string fileExt     = Path.GetExtension(fileName); //获取文件扩展名
                    string newFileName = Guid.NewGuid() + fileExt;    //随机生成新文件名
                    string savePath    = "";                          //完整路径
                    savePath = path + "/" + newFileName;
                    using (var stream = new FileStream(savePath, FileMode.Create))
                    {
                        //写入本地
                        file[0].CopyTo(stream);
                        stream.Flush();
                    }
                    //保存到数据库
                    string filePath = "/wwwroot/Food/" + newFileName;
                    entity.image = filePath;
                }
                else
                {
                    //throw new Exception("请上传一张图片且只能上传一张");
                    var result = new BaseResult <FoodEntity>();
                    result.code = "-1";
                    result.desc = "请上传一张图片且只能上传一张";
                    return(result);
                }
            }

            return(await foodRepository.Add(entity));
        }
Esempio n. 16
0
        public ActionResult <FoodDto> AddFood(ApiVersion version, [FromBody] FoodCreateDto foodCreateDto)
        {
            if (foodCreateDto == null)
            {
                return(BadRequest());
            }

            FoodEntity toAdd = _mapper.Map <FoodEntity>(foodCreateDto);

            _foodRepository.Add(toAdd);

            if (!_foodRepository.Save())
            {
                throw new Exception("Creating a fooditem failed on save.");
            }

            FoodEntity newFoodItem = _foodRepository.GetSingle(toAdd.Id);

            return(CreatedAtRoute(nameof(GetSingleFood), new { version = version.ToString(), id = newFoodItem.Id },
                                  _mapper.Map <FoodDto>(newFoodItem)));
        }
        public IActionResult Add([FromBody] FoodItemViewModel foodItemViewModel)
        {
            try
            {
                if (foodItemViewModel == null)
                {
                    return(BadRequest());
                }

                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                FoodItem newFoodItem = _foodRepository.Add(Mapper.Map <FoodItem>(foodItemViewModel));

                return(CreatedAtRoute("GetSingleFood", new { id = newFoodItem.Id }, Mapper.Map <FoodItemViewModel>(newFoodItem)));
            }
            catch (Exception exception)
            {
                _logger.LogCritical("Error", exception);
                return(new StatusCodeResult(500));
            }
        }
        public ActionResult AddFoodToList([FromBody] FoodItemDto viewModel)
        {
            if (viewModel == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            FoodItem item = _mapper.Map <FoodItem>(viewModel);

            item.Created = DateTime.Now;
            FoodItem newFoodItem = _foodRepository.Add(item);

            _chatHubContext.Clients.All.SendAsync("FoodAdded", newFoodItem);

            return(CreatedAtRoute(
                       nameof(GetSingleFood),
                       new { id = newFoodItem.Id },
                       _mapper.Map <FoodItemDto>(newFoodItem)));
        }
Esempio n. 19
0
        public IActionResult AddFoodToList([FromBody] FoodItemViewModel viewModel)
        {
            if (viewModel == null)
            {
                return(HttpBadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(HttpBadRequest(ModelState));
            }

            FoodItem item = Mapper.Map <FoodItem>(viewModel);

            item.Created = DateTime.Now;
            FoodItem newFoodItem = _foodRepository.Add(item);

            _coolMessageHubContext.Clients.All.FoodAdded(newFoodItem);

            return(CreatedAtRoute(
                       "GetSingleFood",
                       new { foodItemId = newFoodItem.Id },
                       Mapper.Map <FoodItemViewModel>(newFoodItem)));
        }
Esempio n. 20
0
 public Food Add(Food food)
 {
     return(_foodRepository.Add(food));
 }
 public void Add(Food food)
 {
     _foodRepository.Add(food);
 }
Esempio n. 22
0
 public async Task <int> AddFoodAsync(Food food)
 {
     return(await _foodRepo.Add(food));
 }
 public async Task <Food> Add(Food food)
 {
     return(await _foodRepository.Add(food));
 }