Beispiel #1
0
        public async Task <IActionResult> Edit(TagInputViewModel model)
        {
            if (!ModelState.IsValid)
            {
                var errors = this.ModelState.Values.SelectMany(p => p.Errors).Select(e => e.ErrorMessage).ToList();

                var errorModel = this.errorService.GetErrorModel(errors);

                return(View("Error", errorModel));
            }

            try

            {
                await this.tagService.UpdateTag(model);
            }
            catch (Exception e)
            {
                ViewData["Errors"] = e.Message;

                return(this.View("Error"));
            }

            return(RedirectToAction("Index"));
        }
Beispiel #2
0
        public async Task <IActionResult> TagContact([FromBody] TagInputViewModel model, CancellationToken cancellationToken)
        {
            var result = await _contactRepository.TagContactAsync(new UserIdentity().UserId, model.ContactId, model.Tags, cancellationToken);

            if (result)
            {
                return(Ok());
            }
            return(BadRequest());
        }
Beispiel #3
0
        public async Task CreateTag_WithValidModel_ShouldSaveTagToDb()
        {
            //Arrange
            var db   = this.SetDb();
            var repo = new Repository <Tag>(db);

            var service = new TagService(null, repo, this.Mapper);

            var modelA = new TagInputViewModel
            {
                Name = "Cakes",
            };

            //Act
            await service.CreateTag(modelA);

            //Assert
            Assert.NotEmpty(repo.All());
        }
Beispiel #4
0
        public async Task CreateTag_WithDuplicateName_ShouldThrow()
        {
            //Arrange
            var db = this.SetDb();

            await this.SeedTag(db);

            var repo = new Repository <Tag>(db);

            var service = new TagService(null, repo, this.Mapper);

            var modelC = new TagInputViewModel
            {
                Name = "Cakes",
            };

            //Act

            //Assert
            await Assert.ThrowsAsync <InvalidOperationException>(async() => await service.CreateTag(modelC));
        }
Beispiel #5
0
        private async Task SeedTag(CakeItDbContext db)
        {
            var repo = new Repository <Tag>(db);

            var service = new TagService(null, repo, this.Mapper);

            var modelA = new TagInputViewModel
            {
                Name = "Cakes",
            };

            await service.CreateTag(modelA);

            var modelB = new TagInputViewModel
            {
                Name = "Baking"
            };

            await service.CreateTag
                (modelB);
        }
Beispiel #6
0
        public async Task UpdateTag(TagInputViewModel model)
        {
            if (this.tagRepo.All().Any(t => t.Name == model.Name && t.Id != model.Id))
            {
                throw new InvalidOperationException("Tag with the same name already exists.");
            }

            var tag = this.mapper.Map <TagInputViewModel, Tag>(model);

            this.tagRepo.Update(tag);

            try
            {
                await this.tagRepo.SaveChangesAsync();
            }
            catch (Exception e)
            {
                this.logger.LogDebug(e.Message);
                throw new InvalidOperationException("Sorry, can't update tag.");
            }
        }
Beispiel #7
0
        public async Task CreateTag(TagInputViewModel model)
        {
            var tag = this.mapper.Map <TagInputViewModel, Tag>(model);

            if (this.tagRepo.All().Any(t => t.Name == tag.Name && tag.IsDeleted == false))
            {
                throw new InvalidOperationException("Tag with such name alreadey exist in the data base.");
            }

            tagRepo.Add(tag);

            try
            {
                await tagRepo.SaveChangesAsync();
            }
            catch (Exception e)
            {
                this.logger.LogDebug(e.Message);

                throw new InvalidOperationException("Sorry an error accored while creating a tag.");
            }
        }