public async Task <IActionResult> Create([FromBody, Required] DefaultChannel.DefaultChannelCreateData data) { int channelId = data.ChannelId; Channel channel = await _context.Channels.FindAsync(channelId); if (channel == null) { return(NotFound(new ApiError($"The channel with id '{channelId}' was not found."))); } var defaultChannel = new Data.Models.DefaultChannel { Channel = channel, Repository = data.Repository, Branch = data.Branch, Enabled = data.Enabled ?? true }; await _context.DefaultChannels.AddAsync(defaultChannel); await _context.SaveChangesAsync(); return(CreatedAtRoute( new { action = "Get", id = defaultChannel.Id }, new DefaultChannel(defaultChannel))); }
public async Task <IActionResult> Create([FromBody, Required] DefaultChannel.DefaultChannelCreateData data) { int channelId = data.ChannelId; Channel channel = await _context.Channels.FindAsync(channelId); if (channel == null) { return(NotFound(new ApiError($"The channel with id '{channelId}' was not found."))); } Data.Models.DefaultChannel defaultChannel; // Due to abundant retry logic, we'll return a normal response even if this is creating a duplicate, by simply // returning the one that already exists vs. HTTP 409 / 500 var existingInstance = _context.DefaultChannels .Where(d => d.Channel == channel && d.Repository == data.Repository && d.Branch == data.Branch) .FirstOrDefault(); if (existingInstance != null) { defaultChannel = existingInstance; } else { defaultChannel = new Data.Models.DefaultChannel { Channel = channel, Repository = data.Repository, Branch = data.Branch, Enabled = data.Enabled ?? true }; await _context.DefaultChannels.AddAsync(defaultChannel); await _context.SaveChangesAsync(); } return(CreatedAtRoute( new { action = "Get", id = defaultChannel.Id }, new DefaultChannel(defaultChannel))); }