public void Should_ValidateObject()
        {
            //Arrange: Prepare for test
            var dto = new CreateMealDto
            {
                Date = Clock.Now.AddMonths(5), // can't have future date
            };

            //Act: Run SUT
            var ex = Record.Exception(() => _service.ValidateObject(dto));

            //Assert: Check results
            Assert.IsType <AbpValidationException>(ex);
        }
Example #2
0
        public async void ValidateObject(CreateMealDto input)
        {
            CheckCreatePermission();
            //we can use Logger, it's defined in ApplicationService class.
            Logger.Info("Creating a meal for input: " + input);

#if WRONG
            // Creating a new Meal entity with given input's properties
            MapToEntity(input, null);
#else
            var user = await GetCurrentUserAsync();

            // Creating a new Meal entity with given input's properties
            var meal = MapToEntity(input, user);
#endif
        }
        public void Should_SaveValidatedObject()
        {
            //Arrange: Prepare for test
            var dto = new CreateMealDto
            {
                Name        = "TestMeal",
                Date        = Clock.Now,
                Type        = MealType.Breakfast,
                Ingredients = new string[] { }
            };

            //Act: Run SUT
            var ex = Record.Exception(() => _service.SaveValidatedObject(dto));

            //Assert: Check results
            Assert.Null(ex);
        }
Example #4
0
        public async Task <MealDto> CreateWithNames(CreateMealDto input)
        {
            CheckCreatePermission();
            //we can use Logger, it's defined in ApplicationService class.
            Logger.Info("Creating a meal for input: " + input);

            var user = await GetCurrentUserAsync();

            // Creating a new Meal entity with given input's properties
            var meal = MapToEntity(input, user);

            // Saving entity with standard insert method of repositories
            var result = await Repository.InsertAsync(meal);

            var map = MapToEntityDto(result);

            return(map);
        }
Example #5
0
        public async void SaveValidatedObject(CreateMealDto input)
        {
            CheckCreatePermission();
            //we can use Logger, it's defined in ApplicationService class.
            Logger.Info("Creating a meal for input: " + input);

#if WRONG
            throw new NotImplementedException();
#else
            var user = await GetCurrentUserAsync();

            // Creating a new Meal entity with given input's properties
            var meal = MapToEntity(input, user);

            // Saving entity with standard insert method of repositories
            var result = await Repository.InsertAsync(meal);
#endif
        }
Example #6
0
        public async Task <int> CheckIdSavedValidatedObject(CreateMealDto input)
        {
            CheckCreatePermission();
            //we can use Logger, it's defined in ApplicationService class.
            Logger.Info("Creating a meal for input: " + input);

            var user = await GetCurrentUserAsync();

            // Creating a new Meal entity with given input's properties
            var meal = MapToEntity(input, user);

            // Saving entity with standard insert method of repositories
            var result = await Repository.InsertAndGetIdAsync(meal);

#if WRONG
            result--; // get previous
#endif
            return(result);
        }
Example #7
0
        protected Meal MapToEntity(CreateMealDto createInput, User u)
        {
            var meal = Meal.Create(createInput.Name, createInput.Date, createInput.Type);

            meal.AsUser(u);

            if (createInput.Ingredients.Any())
            {
                try
                {
                    var ingredients = _ingredientRepository.GetAll().Where(i => createInput.Ingredients.Contains(i.Name))
                                      .ToList();
                    meal.SetIngredients(ingredients);
                }
                catch (Exception e)
                {
                    throw new UserFriendlyException(e.ToString());
                }
            }

            return(meal);
        }
        public async Task Should_CheckSavedValidatedObject()
        {
            //Arrange: Prepare for test
            // we can work with repositories instead of DbContext
            var mealRepository = LocalIocManager.Resolve <IRepository <Meal> >();
            var dto            = new CreateMealDto
            {
                Name        = "TestMeal",
                Date        = Clock.Now,
                Type        = MealType.Breakfast,
                Ingredients = new string[] { }
            };

            //Act: Run SUT
            // obtain test data
            var mealID = await _service.CheckIdSavedValidatedObject(dto);

            //Assert: Check results
            var meal = mealRepository.Get(mealID);

            meal.ShouldNotBe(null);
            meal.Name.ShouldBe(dto.Name);
        }
Example #9
0
 public IActionResult Post([FromBody] CreateMealDto model) =>
 _mapper.Map <MealInsertModel>(model)
 .Map(_commandRepo.Insert)
 .Map(x => Created(new { id = x }))
 .Reduce(_ => BadRequest(), error => error is ArgumentNotSet)
 .Reduce(_ => InternalServerError(), x => _logger.LogError(x.ToString()));