예제 #1
0
        public async Task <bool> CreateAsync(TagCreateServiceModel model)
        {
            if (!this.IsEntityStateValid(model))
            {
                return(false);
            }

            var nameToCheck = model.Name.Trim().ToLower();

            // Verify that a tag with the provided name does not already exist
            if (await this.Context.Tags.AnyAsync(t => t.Name.ToLower() == nameToCheck))
            {
                return(false);
            }

            var tagDbModel = new Tag
            {
                Name = model.Name.Trim().ToLower()
            };

            await this.Context.Tags.AddAsync(tagDbModel);

            await this.Context.SaveChangesAsync();

            return(true);
        }
예제 #2
0
        public async Task CreateAsync_WithDuplicateName_ReturnsFalse()
        {
            // Arrange
            const string testTagName = "testtag";
            var          context     = this.NewInMemoryDatabase();

            await context.Tags.AddAsync(new Tag
            {
                Name = testTagName
            });

            await context.SaveChangesAsync();

            var serviceModel = new TagCreateServiceModel
            {
                Name = testTagName
            };

            var tagsService = new TagsService(context);

            // Act
            var result = await tagsService.CreateAsync(serviceModel);

            // Assert
            Assert.False(result);
            Assert.True(await context.Tags.AnyAsync(t => t.Name == testTagName));
            Assert.Equal(1, await context.Tags.CountAsync());
        }
예제 #3
0
        public async Task <IActionResult> Create(string name)
        {
            if (name == null)
            {
                return(this.Ok(new { success = false }));
            }

            var serviceModel = new TagCreateServiceModel
            {
                Name = name
            };

            var success = await this.tagsService.CreateAsync(serviceModel);

            return(this.Ok(new { success }));
        }
예제 #4
0
        public async Task CreateAsync_WithInvalidModel_ReturnsFalse()
        {
            // Arrange
            var context = this.NewInMemoryDatabase();

            var serviceModel = new TagCreateServiceModel
            {
                Name = null
            };

            var tagsService = new TagsService(context);

            // Act
            var result = await tagsService.CreateAsync(serviceModel);

            // Assert
            Assert.False(result);
            Assert.Equal(0, await context.Tags.CountAsync());
        }
예제 #5
0
        public async Task CreateAsync_WithValidModel_WorksCorrectly()
        {
            // Arrange
            var context = this.NewInMemoryDatabase();

            var serviceModel = new TagCreateServiceModel
            {
                Name = "TestTag"
            };

            var tagsService = new TagsService(context);

            // Act
            var result = await tagsService.CreateAsync(serviceModel);

            // Assert
            Assert.True(result);
            Assert.True(await context.Tags.AnyAsync(t => t.Name == serviceModel.Name.ToLower()));
        }