コード例 #1
0
        public async Task <IActionResult> Post([FromBody] ChannelEditModel channel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            try
            {
                if (IsNewChannel(channel))
                {
                    Channel model = new Channel();
                    model.ApplyEditChanges(channel);

                    var userId = HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier)?.Value;

                    await _channelService.Create(model, userId);
                }
                else
                {
                    Channel model = _channelService.GetAggregate(channel.Id);
                    model.ApplyEditChanges(channel);
                    await _channelService.Update(model);
                }
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!await ChannelExists(channel.Id))
                {
                    return(NotFound());
                }

                throw;
            }

            return(Ok());

            bool IsNewChannel(ChannelEditModel c) => c.Id <= 0;
        }
コード例 #2
0
        public async Task <IActionResult> Post([FromBody] ChannelEditModel channel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            try
            {
                if (IsNewChannel(channel))
                {
                    Channel model = new Channel();
                    model.ApplyEditChanges(channel);
                    await _channelService.Create(model);
                }
                else
                {
                    Channel model = _channelService.GetAggregate(channel.Id);
                    model.ApplyEditChanges(channel);
                    await _channelService.Update(model);
                }
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!await ChannelExists(channel.Id))
                {
                    return(NotFound());
                }

                throw;
            }

            return(Ok());

            bool IsNewChannel(ChannelEditModel c) => c.Id <= 0;
        }