//[Authorize]
        public async Task <IActionResult> Put(string moniker, int id, [FromBody] SpeakerModel model)
        {
            try
            {
                var speaker = Uow.Speakers.GetSpeaker(id);
                if (speaker == null)
                {
                    return(NotFound());
                }
                if (speaker.Camp.Moniker != moniker)
                {
                    return(BadRequest("Speaker and Camp do not match"));
                }

                //if (speaker.User.UserName != this.User.Identity.Name) return Forbid();

                Mapper.Map(model, speaker);

                if (await Uow.SaveAllAsync())
                {
                    return(Ok(Mapper.Map <SpeakerModel>(speaker)));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"Exception thrown while updating speaker: {ex}");

                return(BadRequest("Could not update speaker"));
            }

            return(BadRequest("Could not update speaker"));
        }
        public async Task <IActionResult> Delete(string moniker, int id)
        {
            try
            {
                var speaker = Uow.Speakers.GetSpeaker(id);
                if (speaker == null)
                {
                    return(NotFound());
                }
                if (speaker.Camp.Moniker != moniker)
                {
                    return(BadRequest("Speaker and Camp do not match"));
                }

                if (speaker.User.UserName != this.User.Identity.Name)
                {
                    return(Forbid());
                }

                Uow.Speakers.Delete(speaker);

                if (await Uow.SaveAllAsync())
                {
                    return(Ok());
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"Exception thrown while deleting speaker: {ex}");
                return(BadRequest("Could not delete speaker"));
            }

            return(BadRequest("Could not delete speaker"));
        }
        public async Task <IActionResult> Delete(string moniker)
        {
            try
            {
                var oldCamp = Uow.Camps.GetCampByMoniker(moniker);
                if (oldCamp == null)
                {
                    return(NotFound($"Could not find a camp with the moniker of {moniker}"));
                }

                Uow.Camps.Delete(oldCamp);

                if (await Uow.SaveAllAsync())
                {
                    return(Ok());
                }
            }
            catch (Exception e)
            {
                _logger.LogError($"Exception thrown while deleting camp: {e}");
                return(BadRequest("Failed to delete camp."));
            }

            return(BadRequest("Failed to delete camp."));
        }
Exemple #4
0
        public async Task <IActionResult> Post(string moniker, int speakerId, [FromBody] TalkModel model)
        {
            try
            {
                var speaker = Uow.Speakers.GetSpeaker(speakerId);
                if (speaker != null)
                {
                    var talk = Mapper.Map <Talk>(model);

                    talk.Speaker = speaker;
                    Uow.Talks.Add(talk);

                    if (await Uow.SaveAllAsync())
                    {
                        AddETag(talk);
                        return(Created(
                                   Url.Link("GetTalk", new { moniker = moniker, speakerId = speakerId, id = talk.Id }),
                                   Mapper.Map <TalkModel>(talk)));
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"Failed to save new talk: {ex}");
            }

            return(BadRequest("Failed to save new talk"));
        }
Exemple #5
0
        public async Task <IActionResult> Delete(string moniker, int speakerId, int id)
        {
            try
            {
                var talk = Uow.Talks.GetTalk(id);
                if (talk == null)
                {
                    return(NotFound());
                }

                if (Request.Headers.ContainsKey("If-Match"))
                {
                    var etag = Request.Headers["If-Match"].First();
                    if (etag != Convert.ToBase64String(talk.RowVersion))
                    {
                        return(StatusCode((int)HttpStatusCode.PreconditionFailed));
                    }
                }

                Uow.Talks.Delete(talk);

                if (await Uow.SaveAllAsync())
                {
                    return(Ok());
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"Failed to delete talk: {ex}");
            }

            return(BadRequest("Failed to delete talk"));
        }
        public async Task <IActionResult> Post(string moniker, [FromBody] SpeakerModel model)
        {
            try
            {
                var camp = Uow.Camps.GetCampByMoniker(moniker);
                if (camp == null)
                {
                    return(BadRequest("Could not find camp"));
                }

                var speaker = Mapper.Map <Speaker>(model);
                speaker.Camp = camp;

                var campUser = await UserManager.FindByNameAsync(User.Identity.Name);

                if (campUser != null)
                {
                    speaker.User = campUser;

                    Uow.Speakers.Add(speaker);

                    if (await Uow.SaveAllAsync())
                    {
                        var url = Url.Link("SpeakerGet", new { moniker = camp.Moniker, id = speaker.Id });
                        return(Created(url, Mapper.Map <SpeakerModel>(speaker)));
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"Exception thrown while adding speaker: {ex}");
                return(BadRequest("Could not add new speaker"));
            }

            return(BadRequest("Could not add new speaker"));
        }
        public async Task <IActionResult> Post([FromBody] CampModel model)
        {
            try
            {
                _logger.LogInformation("Creating new Code Camp");

                var camp = Mapper.Map <Camp>(model);

                Uow.Camps.Add(camp);
                if (await Uow.SaveAllAsync())
                {
                    var newUri = Url.Link("CampGet", new { moniker = camp.Moniker });
                    return(Created(newUri, Mapper.Map <CampModel>(camp)));
                }

                _logger.LogError("Could not save camp to database.");
                return(BadRequest("Failed to save camp."));
            }
            catch (Exception e)
            {
                _logger.LogError($"Exception thrown while saving camps: {e}");
                return(BadRequest("Failed to created camp."));
            }
        }
        [HttpPut("{moniker}")]   //Update entire object
        public async Task <IActionResult> Put(string moniker, [FromBody] CampModel model)
        {
            try
            {
                var oldCamp = Uow.Camps.GetCampByMoniker(moniker);
                if (oldCamp == null)
                {
                    return(NotFound($"Could not find a camp with the moniker of {moniker}"));
                }

                Mapper.Map(model, oldCamp);
                if (await Uow.SaveAllAsync())
                {
                    return(Ok(Mapper.Map <CampModel>(oldCamp)));
                }
            }
            catch (Exception e)
            {
                _logger.LogError($"Exception thrown while updating camp: {e}");
                return(BadRequest("Failed to update camp."));
            }

            return(BadRequest("Failed to update camp."));
        }