Beispiel #1
0
        public async Task <int> Handle(AdaugaNotaCommand message)
        {
            try
            {
                if (message.IdIntrebare.HasValue && message.IdIntrebare.Value > 0)
                {
                    var existaIntrebare = await _context.Questions.AnyAsync(i => i.Id == message.IdIntrebare.Value);

                    if (!existaIntrebare)
                    {
                        throw new ArgumentException("Intrebarea nu exista");
                    }
                }

                var nota = _mapper.Map <Note>(message);
                _context.Add(nota);

                return(await _context.SaveChangesAsync());
            }
            catch (Exception ex)
            {
                _logger.LogError(new EventId(), ex.Message);
            }

            return(-1);
        }
Beispiel #2
0
        public async Task <IActionResult> Create([Bind("PersonID,Name,Count")] Person person)
        {
            if (ModelState.IsValid)
            {
                _context.Add(person);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(person));
        }
        public async Task <int> Handle(InregistreazaSectieCommand message)
        {
            try
            {
                //TODO[DH] this can be moved to a previous step, before the command is executed
                int idSectie = await _context.SectieDeVotare
                               .Where(a =>
                                      a.NumarSectie == message.NumarSectie &&
                                      a.IdJudetNavigation.CodJudet == message.CodJudet).Select(a => a.IdSectieDeVotarre)
                               .FirstOrDefaultAsync();

                if (idSectie == 0)
                {
                    throw new ArgumentException("Sectia nu exista");
                }

                var formular = await _context.RaspunsFormular
                               .FirstOrDefaultAsync(a =>
                                                    a.IdObservator == message.IdObservator &&
                                                    a.IdSectieDeVotare == idSectie);

                if (formular == null)
                {
                    formular = _mapper.Map <RaspunsFormular>(message);

                    formular.IdSectieDeVotare = idSectie;

                    _context.Add(formular);
                }
                else
                {
                    _mapper.Map(message, formular);
                }

                return(await _context.SaveChangesAsync());
            }
            catch (Exception ex)
            {
                _logger.LogError(new EventId(), ex.Message);
            }

            return(-1);
        }
        protected override async Task <int> HandleCore(InregistreazaSectieCommand message)
        {
            try
            {
                //TODO[DH] this can be moved to a previous step, before the command is executed
                int idSectie = await _context.PollingStations
                               .Where(a =>
                                      a.Number == message.NumarSectie &&
                                      a.County.Code == message.CodJudet).Select(a => a.Id)
                               .FirstOrDefaultAsync();

                if (idSectie == 0)
                {
                    throw new ArgumentException("Sectia nu exista");
                }

                var formular = await _context.PollingStationInfos
                               .FirstOrDefaultAsync(a =>
                                                    a.IdObserver == message.IdObservator &&
                                                    a.IdPollingStation == idSectie);

                if (formular == null)
                {
                    formular = _mapper.Map <PollingStationInfo>(message);

                    formular.IdPollingStation = idSectie;

                    _context.Add(formular);
                }
                else
                {
                    _mapper.Map(message, formular);
                }

                return(await _context.SaveChangesAsync());
            }
            catch (Exception ex)
            {
                _logger.LogError(new EventId(), ex.Message);
            }

            return(-1);
        }
        private List <Weather> SortWeathers(List <Weather> weatherList)
        {
            var votesList = _context.Votes.Include(x => x.Supplier).
                            ToList();

            foreach (var w in weatherList)
            {
                if (!votesList.Any(x => x.Supplier.Name == w.Supplier.Name))
                {
                    _context.Add(new Vote
                    {
                        Supplier = new WeatherSupplier
                        {
                            Name = w.Supplier.Name
                        },
                        Likes    = 0,
                        Location = w.Loc.CityName
                    });
                    w.Sorting = 0;
                }
                else
                {
                    w.Sorting = votesList.First(x => x.Supplier.Name == w.Supplier.Name).Likes;
                }
            }
            _context.SaveChanges();

            votesList = _context.Votes.Include(x => x.Supplier).ToList().OrderByDescending(x => x.Likes).ToList();
            var sortedWeather = new List <Weather>();

            foreach (var vote in votesList)
            {
                sortedWeather.Add(weatherList.First(x => x.Supplier.Name == vote.Supplier.Name));
            }
            return(sortedWeather);
        }
Beispiel #6
0
 public async Task Post([FromBody] Poll poll)
 {
     _votingContext.Add(poll);
     await _votingContext.SaveChangesAsync();
 }