GithubHelp home page GithubHelp logo

votingapp's Introduction

Aplikacja do głosowania, tworzenia ankiet, sprawdzania ich statystyk(todo) oraz generowania raportów(todo). Korzystanie z usług wymaga rejestracji, zaimplementowanej z wykorzystaniem ASP.NET Core Identity.


Wykorzystane technologie i rozwiązania

  • ASP .NET Core 2.0 (wzorzec projektowy MVC)
  • Razor Views + Bootstrap (prosty front aplikacji)
  • npm (manager pakietów jquery,bootstrap)
  • Entity Framework Core (Code First, obsługa baz danych MSSQL)
  • ASP .NET Core Identity (autoryzacja/autentykacja)
  • DbInitializer.cs do utworzenia startowych kont i ankiet
  • LoginStatusViewComponent do prezentacji belki logowania/zarządzania ankietami dla zalogowanych, zgodnie z tym artykułem
  • wykorzystanie nuget pakietu OdeToCode.AddFeatureFolders od K. Scott Allena (opisane tutaj) do organizacji controllerów i views wewnątrz nowego folderu Features


Struktura solucji projektu

Struktura reprezentuje moją pierwszą próbę zastosowania się do Clean Architecture bazując na wskazówkach opisanych w Architecting Modern Web Apps with ASP.NET Core 2 and Azure

  • VotingApp.Core zawiera klasy POCO modeli (Answer, Poll, Vote, ApplicationUser) oraz interfejsy (IPoll, IVote)
  • VotingApp.Infrastructure zawiera DbContext i migracje z EF Core oraz implementacje interfejsów (/Services)
  • VotingApp.Web - warstwa prezentacyjna z views i controllers pogrupowanymi w dodatkowym folderze Features

struktura projektu



Dodawanie ankiety

Każdy zalogowany użytkownik może dodać ankietę. Pola formularza: pytanie, odpowiedzi, status (public/private).

Private - ankieta niewidoczna na liście ankiet, dostępna jedynie z adresu

Dodawanie ankiety

Przycisk "Add new answer field" dodaje, z użyciem javascriptu, kolejne pole na odpowiedź. "Remove" zaś usuwa powiązane do siebie pole. Poniżej kod zawarty w AddPoll.cshtml

    $(document).ready(function () {
        $('.answers_wrapper').on("click", ".remove_button", function (e) { //user click on remove text
            e.preventDefault(); $(this).parent('div').parent('div').remove();
        })

        var answerHTML = '<div class="input-group col-8 mb-3"><input class="form-control" type="text" data-val="true" data-val-required="The Answers field is required." id="Answers" name="Answers" /><div class="input-group-append"> <button class="btn btn-outline-secondary remove_button" type="button">Remove</button></div></div>';
        $(".add_button").click(function () {
            $(".answers_wrapper").append(answerHTML);
        });

    });

W ViewModelu PollAddViewModel.cs został również dopisany customowy atrybut, zapewniający że lista odpowiedzi w formularzu nie będzie pusta oraz nie będzie zawierała pustych elementów.

public class PollAddViewModel
    {
        [Required]
        public string Question { get; set; }
        [EnsureICollectionElementNotNull(ErrorMessage = "Answer cannot be empty.")]
        public ICollection<string> Answers { get; set; }
        public PollStatus Status { get; set; }
    }

    public class EnsureICollectionElementNotNullAttribute : ValidationAttribute
    {
        public override bool IsValid(object value)
        {
            var list = value as ICollection<string>;
            if (list == null) return false;

            if (list.Any(answer => answer == null))
            {
                return false;
            }
            return true;
        }
    }

Poniżej metoda HttpPostAddPoll() w controllerze odpowiadająca za dodawanie ankiety.

        [HttpPost, ValidateAntiForgeryToken]
        public async Task<IActionResult> AddPoll([Bind("Question,Answers,Status")] PollAddViewModel model)
        {
            if (ModelState.IsValid)
            {
                Poll newPoll = new Poll
                {
                    Question = model.Question,
                    Answers = model.Answers.Select(s => new Answer
                    {
                        Description = s,
                    }).ToList(),
                    Status = model.Status,
                    User = await _userManager.GetUserAsync(User)
                };

                _polls.Add(newPoll);
                return RedirectToAction(nameof(MyPollsItem), new { pollId = newPoll.PollId });
            }

            return View(model);
        }


Głosowanie

Głosowanie wymaga bycia zalogowanym, głos można oddać raz, ostatecznie (bez możliwości zmiany).

głosowanie na ankietę

Wyniki ankiety

Widok po zagłosowaniu, wyświetla ilość głosów oraz Pie Chart z Google Charts

Wyniki ankiety

Metoda zwracająca dane json z wynikami ankiety
[AllowAnonymous]
        [HttpGet]
        [Route("[controller]/[action]/{pollId}")]
        public JsonResult ResultsPieChart(int pollId)
        {
            var answers = _polls.GetPollById(pollId).Answers;

            var resultsData = answers.Select(a => new {
                    description = a.Description,
                    votes = a.Votes
                }).ToList();
            return Json(resultsData);
        }


Zarządzanie ankietami

Lista własnych ankiet, z przyciskiem usuwającym

Lista ankiet

Panel zarządzający ankiety

panel ankiety

"Get summary in .PDF" - TODO

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.