Exemple #1
0
        public JsonResult InsertOrUpdate(ElectionHead input)
        {
            ResultDto <int> result = new ResultDto <int>();

            try
            {
                using (ElectionService ObjServ = new ElectionService())
                {
                    if (input.Id == 0)
                    {
                        input.CreationTime = DateTime.Now;
                    }
                    input.TenantId = TenantId;
                    ObjServ.ReposityHD.InsertOrUpdate(input);
                    result.code    = 100;
                    result.datas   = input.Id;
                    result.message = "ok";
                }
            }
            catch (Exception ex)
            {
                result.code    = 500;
                result.message = ex.Message;
            }
            return(Json(result));
        }
Exemple #2
0
        public JsonResult GetPageLists(int page, int pageSize, string keywords = "")
        {
            ResultDto <List <Dto.ElectionHdDto> > result = new ResultDto <List <Dto.ElectionHdDto> >();

            try
            {
                ResultDto <List <ElectionHead> > items = new ResultDto <List <ElectionHead> >();
                ElectionService ObjServ = new ElectionService();
                if (!string.IsNullOrEmpty(keywords))
                {
                    items = ObjServ.ReposityHD.GetPageList(page, pageSize, o => (o.Title.Contains(keywords) && o.TenantId == TenantId));
                }
                else
                {
                    items = ObjServ.ReposityHD.GetPageList(page, pageSize, o => o.TenantId == TenantId);
                }
                result = Mapper.Map <ResultDto <List <Dto.ElectionHdDto> > >(items);
                ObjServ.Dispose();
            }
            catch (Exception ex)
            {
                result.code    = 500;
                result.message = ex.Message;
            }
            return(Json(result));
        }
        public async void RemoveElectionUnitTest()
        {
            // Arrange
            var mockElectionRepository = GetElectionRepositoryMock();
            var deleteElectionSetup    = new RemoveElectionSetup();

            var election = new Election
            {
                Id             = 1,
                Ballots        = new[] { new Ballot() },
                CreatedDate    = DateTime.Now,
                ElectionQr     = Guid.NewGuid(),
                ExpirationDate = DateTime.Now.Add(new TimeSpan(7, 0, 0, 0))
            };

            deleteElectionSetup.SetupMock(mockElectionRepository);
            var electionService = new ElectionService(mockElectionRepository.Object);

            mockElectionRepository.Verify(repo => repo.RemoveAsync(It.IsAny <Election>()), Times.AtMostOnce);

            // Act
            await electionService.RemoveAsync(election);

            // Assert

            //Verify if repository is called. see the code above
        }
        public async void CreateDefaultElectionUnitTest()
        {
            // Arrange
            string templatePath           = $".{Path.DirectorySeparatorChar}ElectionTemplate.json";
            var    mockElectionRepository = GetElectionRepositoryMock();

            mockElectionRepository.Setup(repo => repo.AddAsync(It.IsAny <Election>()))
            .ReturnsAsync((Election election) => election);

            var electionService = new ElectionService(mockElectionRepository.Object);

            var mockElectionTemplateLoader = new Mock <IElectionJsonTemplateLoader>();

            mockElectionTemplateLoader.Setup(el => el.Load(It.IsAny <string>()))
            .Returns((string str) =>
                     new Election
            {
                ElectionQr = Guid.NewGuid()
            });


            // Act
            var result = await electionService.CreateElectionUsingTemplateAsync(templatePath, mockElectionTemplateLoader.Object);

            // Assert
            mockElectionTemplateLoader.Verify(loader => loader.Load(It.IsAny <string>()), Times.AtMostOnce);

            Assert.NotNull(result);
            await Assert.ThrowsAsync <TemplateNotFoundException>(async() => await electionService.CreateElectionUsingTemplateAsync("test", mockElectionTemplateLoader.Object));
        }
        public JsonResult DoVote(string ids)
        {
            ResultDto <long> result = new ResultDto <long>();

            try
            {
                if (MemberId <= 0)
                {
                    result.code    = 500;
                    result.message = "请先登录再投票";
                }
                else if (Member.UserID == "00")
                {
                    result.code    = 500;
                    result.message = "对不起,您不是户籍人员,暂时不能投票";
                }
                else
                {
                    ElectionService ObjServ = new ElectionService();
                    string[]        idarr   = ids.Split(',');
                    foreach (string item in idarr)
                    {
                        int id     = int.Parse(item);
                        var entity = ObjServ.Reposity.Get(id);
                        entity.Voter = entity.Voter == null ? "admin" : entity.Voter;
                        if (!entity.IsOpen)
                        {
                            result.code    = 500;
                            result.message = "投票通道已经关闭";
                        }
                        else if (entity.EndTime < DateTime.Now)
                        {
                            result.code    = 500;
                            result.message = "选举已经过期";
                        }
                        else if (entity.Voter.Contains(Member.UserName))
                        {
                            result.code    = 500;
                            result.message = "您已经投票过了哦";
                        }
                        else
                        {
                            entity.NumOfVote += 1;
                            entity.Voter     += "," + Member.UserName;
                            ObjServ.Reposity.InsertOrUpdate(entity);
                            result.code    = 100;
                            result.message = "ok";
                            result.datas   = entity.Id;
                        }
                    }
                    ObjServ.Dispose();
                }
            }
            catch (Exception ex)
            {
                result.code    = 500;
                result.message = ex.Message;
            }
            return(Json(result));
        }
        [HttpGet] //gets all electoral rooms + candidates
        public IActionResult GetAll()
        {
            //var electoralRooms = Repository.GetAll();
            var electoralRooms = ElectionService.GetAllElectoralRooms();

            //var electoralRoomDtos = Mapper.Map<IList<ElectionView>>(electoralRoom);
            return(Ok(electoralRooms));
        }
Exemple #7
0
        public override void Setup()
        {
            base.Setup();
            CategoryService     categoryService     = new CategoryService();
            PartyService        partyService        = new PartyService();
            TicketService       ticketService       = new TicketService();
            CategoryTypeService categoryTypeService = new CategoryTypeService();

            electionService = new ElectionService(categoryService, categoryTypeService, partyService, ticketService);
        }
 public ProfileController(
     ProfileService profileService,
     WalletService walletService,
     ElectionService electionService
     )
 {
     _walletService   = walletService;
     _profileService  = profileService;
     _electionService = electionService;
 }
        public async void RemoveAllExpiredElectionUnitTest()
        {
            // Arrange
            var mockRepo = GetElectionRepositoryMock();
            var service  = new ElectionService(mockRepo.Object);

            // Act
            await service.RemoveAllExpiredElectionsAsync();

            // Assert
            mockRepo.Verify(repo => repo.RemoveAsync(It.IsAny <Election>()), Times.AtMostOnce);
        }
        public async void SearchElectionUnitTest()
        {
            // Arrange
            var mockRepo = GetElectionRepositoryMock();

            var service = new ElectionService(mockRepo.Object);

            // Act
            await service.SearchAsync(new ElectionSearchRequest());

            // Assert
            mockRepo.Verify(repo => repo.SearchAsync(It.IsAny <Expression <Func <Election, bool> > >()), Times.AtMostOnce);
        }
        public JsonResult GetLists(int page, int pageSize, int electionHdId)
        {
            ElectionService ObjServ = new ElectionService();
            var             items   = ObjServ.Reposity.GetPageList(page, pageSize, o => o.IsOpen && o.TenantId == tenant.Id && o.ElectionHdId == electionHdId);
            var             result  = Mapper.Map <ResultDto <List <Dto.ElectionDto> > >(items);

            if (result.datas != null)
            {
                result.datas = result.datas.OrderByDescending(o => o.CreationTime).ToList();
            }
            ObjServ.Dispose();
            return(Json(result));
        }
Exemple #12
0
        public JsonResult GetModel(int id)
        {
            ElectionHead    model   = new ElectionHead();
            ElectionService ObjServ = new ElectionService();
            var             result  = ObjServ.ReposityHD.FirstOrDefault(id);

            if (result != null)
            {
                model = result;
            }
            var entity = Mapper.Map <Dto.ElectionHdDto>(model);

            ObjServ.Dispose();
            return(Json(entity));
        }
        public JsonResult GetVoteItems(int page, int pageSize)
        {
            ResultDto <List <Dto.ElectionHdDto> > result = new ResultDto <List <Dto.ElectionHdDto> >();

            try
            {
                ElectionService ObjServ = new ElectionService();
                var             items   = ObjServ.ReposityHD.GetPageList(page, pageSize, o => o.TenantId == tenant.Id && o.EndTime > DateTime.Now && o.IsOpen);
                result = Mapper.Map <ResultDto <List <Dto.ElectionHdDto> > >(items);
                ObjServ.Dispose();
            }
            catch (Exception ex)
            {
                result.code    = 500;
                result.message = ex.Message;
            }
            return(Json(result));
        }
Exemple #14
0
        public JsonResult Del(int id)
        {
            ResultDto <string> result = new ResultDto <string>();

            try
            {
                ElectionService ObjServ = new ElectionService();
                ObjServ.ReposityHD.Delete(id);
                result.code    = 100;
                result.message = "success";
                ObjServ.Dispose();
            }
            catch (Exception ex)
            {
                result.code    = 500;
                result.message = ex.Message;
            }
            return(Json(result));
        }
        public async void CreateCustomElectionUnitTest()
        {
            // Arrange
            var mockElectionRepository = GetElectionRepositoryMock();
            var createElectionSetup    = new CreateElectionSetup();

            createElectionSetup.SetupMock(mockElectionRepository);
            var election = new Election
            {
                ElectionQr  = Guid.NewGuid(),
                CreatedDate = DateTime.UtcNow
            };
            var electionService = new ElectionService(mockElectionRepository.Object);

            // Act
            Election result = await electionService.AddAsync(election);

            // Assert
            Assert.True(result.Id > 0);
            Assert.Null(result.Ballots);
        }
Exemple #16
0
        public JsonResult DoOpen(int id)
        {
            ResultDto <int> result = new ResultDto <int>();

            try
            {
                using (ElectionService ObjServ = new ElectionService())
                {
                    var input = ObjServ.ReposityHD.FirstOrDefault(id);
                    input.IsOpen = input.IsOpen ? false : true;
                    ObjServ.ReposityHD.InsertOrUpdate(input);
                    result.code    = 100;
                    result.datas   = input.Id;
                    result.message = "ok";
                }
            }
            catch (Exception ex)
            {
                result.code    = 500;
                result.message = ex.Message;
            }
            return(Json(result));
        }
 public BaseController()
 {
     Service = new ElectionService();
 }
 public ElectionController(ElectionService electionService, IHttpContextAccessor contextAccessor)
 //  : base(contextAccessor)
 {
     _electionService = electionService;
 }
 public ElectionsController(ElectionService electionService)
 {
     _electionService = electionService;
 }
Exemple #20
0
 public CandidateController(ElectionService electionService, IHttpContextAccessor contextAccessor) : base(
         contextAccessor)
 {
     _electionService = electionService;
 }
 public ElectionController(VotingService votingService, ElectionService electionService, IHttpContextAccessor contextAccessor)
     : base(contextAccessor)
 {
     _electionService = electionService;
     _votingService   = votingService;
 }