Ejemplo n.º 1
0
        public async Task <IActionResult> UpsertSponsors(string moniker, [FromBody] SponsorViewModel vm)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var sponsor = _repo.GetSponsor(vm.Id);

                    if (sponsor == null) // new
                    {
                        sponsor = _mapper.Map <Sponsor>(vm);
                        var eventInfo = _repo.GetEventInfo(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"));
        }