Пример #1
0
        public async Task <ActionResult <Campaign> > PostCampaign([FromBody] Campaign campaign)
        {
            try
            {
                campaign.UserId = User.FindFirstValue(ClaimTypes.NameIdentifier);

                if (campaign.CampaignName is null || campaign.CampaignName == "")
                {
                    campaign.CampaignName = "Your Campaign";
                }

                campaign.CampaignCreateDate  = Utilities.NewCreateDateFormatted();
                campaign.CampaignDescription = "Click the edit button to make a description for this campaign.";

                await _context.AddAsync(campaign);

                await _context.SaveChangesAsync();

                await SetCampaignToSelected(campaign.CampaignID);

                return(CreatedAtAction("GetCampaign", new { id = campaign.CampaignID }, campaign));
            }
            catch (Exception exc)
            {
                _logger.AddSystemLog("Error", $"Unable to create new campaign: {exc}");
                return(BadRequest());
            }
        }
Пример #2
0
        public async Task <IActionResult> PutAdventureLog(int id, AdventureLog sentAdventureLog)
        {
            string       requestingUser = User.FindFirstValue(ClaimTypes.NameIdentifier);
            AdventureLog adventureLog   = await _context.AdventureLogs.FindAsync(id);

            if (id != sentAdventureLog.AdventureLogID || adventureLog.UserId != requestingUser)
            {
                return(BadRequest());
            }
            if (!string.IsNullOrWhiteSpace(sentAdventureLog.LogTitle))
            {
                adventureLog.LogTitle = sentAdventureLog.LogTitle;
            }
            if (!string.IsNullOrWhiteSpace(sentAdventureLog.LogBody))
            {
                adventureLog.LogBody = sentAdventureLog.LogBody;
            }

            _context.Entry(adventureLog).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!AdventureLogExists(id))
                {
                    _logController.AddSystemLog($"WARNING: User {requestingUser} has caused a DbUpdateConcurrencyException");
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }