Ejemplo n.º 1
0
        public async Task <IActionResult> Insert(string moniker, [FromBody] EventInfoViewModel vm)
        {
            try
            {
                if (moniker != vm.Moniker)
                {
                    return(BadRequest("Wrong Event with Moniker"));
                }
                var info = await _repo.GetEventInfoAsync(vm.Moniker);

                if (info != null)
                {
                    return(BadRequest("Cannot add duplicate moniker"));
                }
                else
                {
                    info          = _mapper.Map <EventInfo>(vm);
                    info.Name     = $"Event Name for {vm.Moniker}";
                    info.Location = new EventLocation();
                }

                _repo.AddOrUpdate(info);
                await _repo.SaveChangesAsync();

                return(Created($"/api/events/{info.Moniker}", _mapper.Map <EventInfoViewModel>(info)));
            }
            catch (Exception ex)
            {
                _logger.LogError("Failed to upsert EventInfo. {0}", ex);
            }

            return(BadRequest("Failed to save new event."));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Create(string moniker, [FromBody] TimeSlot model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var eventInfo = await _repo.GetEventInfoAsync(moniker);

                    if (eventInfo != null)
                    {
                        model.Event = eventInfo;

                        _repo.AddOrUpdate(model);
                        await _repo.SaveChangesAsync();

                        return(Created($"{moniker}/api/timeslot/{model.Id}", model));
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError("Failed to create a timeslot: {0}", ex);
                }
            }

            return(BadRequest("Failed to save TimeSlot"));
        }
        async Task <IActionResult> UpsertSpeaker(SpeakerViewModel model, Speaker speaker, string moniker, string userName)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    if (speaker == null)
                    {
                        speaker          = _mapper.Map <Speaker>(model);
                        speaker.UserName = userName;
                        speaker.Event    = await _repo.GetEventInfoAsync(moniker);

                        // Send confirmation email on new speaker
                        var user = await _userMgr.FindByNameAsync(userName);

                        var speakerUrl = this.Url.Link("MySpeakerPage", new { moniker = moniker });
                        await _mailService.SendTemplateMailAsync(
                            "SpeakerSignUp",
                            new SpeakerModel()
                        {
                            Name       = user.Name,
                            Email      = user.Email,
                            Subject    = $"Speaking at the {speaker.Event.Name}",
                            Speaker    = speaker,
                            SpeakerUrl = speakerUrl,
                            Event      = speaker.Event
                        });
                    }
                    else
                    {
                        _mapper.Map <SpeakerViewModel, Speaker>(model, speaker);
                    }

                    _repo.AddOrUpdate(speaker);
                    await _repo.SaveChangesAsync();

                    return(Ok(_mapper.Map <SpeakerViewModel>(speaker)));
                }
                catch (Exception ex)
                {
                    _logger.LogError("Failed to get update speaker: {0}", ex);
                    ModelState.AddModelError("", $"Failed to Save: {ex.Message}");
                }
            }
            _logger.LogError("Failed to get update speaker because of bad Model State: {0}", ModelState);
            return(BadRequest($"Failed to save Speaker: ModelState has Errors: #{ModelState.ErrorCount}"));
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> UpsertSponsors(string moniker, [FromBody] SponsorViewModel vm)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var sponsor = await _repo.GetSponsorAsync(vm.Id);

                    if (sponsor == null) // new
                    {
                        sponsor = _mapper.Map <Sponsor>(vm);
                        var eventInfo = await _repo.GetEventInfoAsync(moniker);

                        sponsor.Event = eventInfo;
                        _repo.AddOrUpdate(sponsor);

                        await _repo.SaveChangesAsync();

                        return(Created($"/{moniker}/api/sponsors/{vm.Id}", _mapper.Map <SponsorViewModel>(sponsor)));
                    }
                    else
                    {
                        _mapper.Map <SponsorViewModel, Sponsor>(vm, sponsor);
                        _repo.AddOrUpdate(sponsor);
                        await _repo.SaveChangesAsync();

                        return(Ok(vm));
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError("Failed to update sponsor: {0}", ex);
                    ModelState.AddModelError("", $"Failed to Save: {ex.Message}");
                }
            }

            return(BadRequest("Failed to save Sponsor"));
        }