Exemple #1
0
        public int InsertVote(TVote vote)
        {
            if (vote == null)
            {
                return(0);
            }

            if (vote.CatId <= 0)
            {
                _logger.LogError($"Invalid Vote. WinCatId: {vote.CatId}");
                throw new Exception();
            }

            if (_catMashDbContext.TCatPicture.Where(c => c.Id == vote.CatId).Count() <= 0)
            {
                _logger.LogError($"Cat Not Found for this Vote. CatId : {vote.CatId}");
                throw new Exception();
            }

            try
            {
                vote.CreationDate = DateTime.UtcNow;
                _catMashDbContext.TVote.Add(vote);

                return(_catMashDbContext.SaveChanges());
            }
            catch (Exception exp)
            {
                _logger.LogError("DataAccessException");
                throw new Exception("DataAccessException", exp);
            }
        }
            public async Task <int> Handle(CreateVoteCommand request, CancellationToken cancellationToken)
            {
                var Vote = new TVote();

                Vote.Decision        = request.Decision;
                Vote.DeputyId        = request.DeputyId;
                Vote.LawsAmendmentId = request.LawsAmendemntId;

                _context.Votes.Add(Vote);
                await _context.SaveChangesAsync();

                return(Vote.Id);
            }
Exemple #3
0
        public void Should_AddVote_Add_Vote_When_Is_Correct()
        {
            var options = new DbContextOptionsBuilder <CatMashDbContext>()
                          .UseInMemoryDatabase(databaseName: "Should_AddVote_Add_Vote_When_Is_Correct")
                          .Options;

            using (var context = new CatMashDbContext(options))
            {
                context.TCatPicture.Add(new TCatPicture {
                    Id = 1, Url = "test.com/test1"
                });
                context.TCatPicture.Add(new TCatPicture {
                    Id = 2, Url = "test.com/test2"
                });
                context.TCatPicture.Add(new TCatPicture {
                    Id = 3, Url = "test.com/test3"
                });
                context.TCatPicture.Add(new TCatPicture {
                    Id = 4, Url = "test.com/test4"
                });
                context.TCatPicture.Add(new TCatPicture {
                    Id = 5, Url = "test.com/test5"
                });
                context.TCatPicture.Add(new TCatPicture {
                    Id = 6, Url = "test.com/test6"
                });
                var updateCount = context.SaveChanges();

                Assert.AreEqual(6, updateCount);
            }

            using (var context = new CatMashDbContext(options))
            {
                var mock = new Mock <ILogger <VoteRepository> >();
                ILogger <VoteRepository> logger = mock.Object;

                logger = Mock.Of <ILogger <VoteRepository> >();

                var catRepository = new VoteRepository(context, logger);

                var vote = new TVote {
                    CatId = 1
                };

                var updateCount = catRepository.InsertVote(vote);
                Assert.AreEqual(1, updateCount);
            }
        }