Exemplo n.º 1
0
        public async Task <MainCategoryServiceModel> CreateAsync(MainCategoryServiceModel serviceModel)
        {
            MainCategory mainCategory = serviceModel.To <MainCategory>();

            await this.context.MainCategories.AddAsync(mainCategory);

            await this.context.SaveChangesAsync();

            return(serviceModel);
        }
Exemplo n.º 2
0
        public async Task <MainCategoryServiceModel> EditAsync(MainCategoryServiceModel mainCategoryServiceModel)
        {
            MainCategory mainCategoryFromDb = this.context.MainCategories
                                              .Find(mainCategoryServiceModel.Id);

            mainCategoryFromDb.Name = mainCategoryServiceModel.Name;

            this.context.MainCategories.Update(mainCategoryFromDb);
            await this.context.SaveChangesAsync();

            return(mainCategoryFromDb.To <MainCategoryServiceModel>());
        }
        public async void GetMainCategoryByIdShouldReturnServiceModelFromDatabaseById()
        {
            var options = new DbContextOptionsBuilder <TechAndToolsDbContext>()
                          .UseInMemoryDatabase(databaseName: "GetMainCategoryByIdShouldReturnServiceModelFromDatabaseById")
                          .Options;

            var context = new TechAndToolsDbContext(options);

            await SeedData(context);

            IMainCategoryService mainCategoryService = new MainCategoryService(context);

            MainCategory expectedData = context.MainCategories.First();

            MainCategoryServiceModel actualData = mainCategoryService.GetMainCategoryById(expectedData.Id);

            ;
            Assert.Equal(expectedData.Name, actualData.Name);
            Assert.Equal(expectedData.Id, actualData.Id);
            Assert.NotNull(actualData.Categories);
            Assert.Equal(expectedData.Categories.Count, actualData.Categories.Count);
        }
        public async void EditAsyncShouldEditMainCategoryFromDatabaseById()
        {
            var options = new DbContextOptionsBuilder <TechAndToolsDbContext>()
                          .UseInMemoryDatabase(databaseName: "EditAsyncShouldEditMainCategoryFromDatabaseById")
                          .Options;

            var context = new TechAndToolsDbContext(options);

            await SeedData(context);

            IMainCategoryService mainCategoryService = new MainCategoryService(context);

            MainCategory mainCategoryFromDb = context.MainCategories.First();

            mainCategoryFromDb.Name = "New Name";

            MainCategoryServiceModel expectedResult = mainCategoryFromDb.To <MainCategoryServiceModel>();

            await mainCategoryService.EditAsync(expectedResult);

            var actualResult = context.MainCategories.First();

            Assert.Equal(expectedResult.Name, actualResult.Name);
        }