Example #1
0
        public async Task <ActionResult <TChannelModel> > Post(TChannelModel model)
        {
            try
            {
                var existing = await _repository.GetTChannelAsync(model.Id);

                if (existing != null)
                {
                    return(BadRequest("Id in use"));
                }

                //var location = _linkGenerator.GetPathByAction("Get", "TChannels",new  { id = model.Id });

                //if (string.IsNullOrWhiteSpace(location)) {
                //    return BadRequest("Could not use current Moniker");
                //}

                // Create a new tChannel
                var tChannel = _mapper.Map <TChannel>(model);
                _repository.Add(tChannel);

                if (await _repository.SaveChangesAsync())
                {
                    return(Created($"/api/tChannels/{tChannel.ChannelName}", _mapper.Map <TChannelModel>(tChannel)));
                }
            }
            catch (Exception)
            {
                return(this.StatusCode(StatusCodes.Status500InternalServerError, "Database failure"));
            }
            return(BadRequest());
        }
Example #2
0
        public async Task <ActionResult <TChannelModel> > Put(string id, TChannelModel model)
        {
            try
            {
                var record = await _repository.GetTChannelAsync(id, false);

                if (record == null)
                {
                    return(NotFound("Couldn't find the record"));
                }


                _mapper.Map(model, record);

                if (await _repository.SaveChangesAsync())
                {
                    return(_mapper.Map <TChannelModel>(record));
                }
                return(BadRequest());
            }
            catch (Exception)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Failed to connect to database"));
            }
        }
Example #3
0
        public async Task <ActionResult <TChannelModel> > Post(string id, TChannelModel model)
        {
            try
            {
                var tChannel = _mapper.Map <TChannel>(model);

                var channelInRepo = await _repository.GetTChannelAsync(id, false);

                if (channelInRepo != null)
                {
                    return(BadRequest("Record already exists"));
                }

                var channelInRepoFromBody = await _repository.GetTChannelAsync(model.Id, false);

                if (channelInRepoFromBody != null)
                {
                    return(BadRequest($"Record {model.Id.ToString()} already exists"));
                }

                _repository.Add(tChannel);

                if (await _repository.SaveChangesAsync())
                {
                    var url = _linkGenerator.GetPathByAction(HttpContext, "Get", values: new
                    {
                        id,
                        i1d = tChannel.Id
                    });
                    return(Created(url, _mapper.Map <TChannelModel>(tChannel)));
                }
                else
                {
                    return(BadRequest("Failed to save new record."));
                }
            }
            catch (Exception)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Failed to connect to database"));
            }
        }