Ejemplo n.º 1
0
        public IHttpActionResult AddPigeon(PigeonBindingModel inputPigeon)
        {
            if (inputPigeon == null)
            {
                return this.BadRequest(InvalidPigeonPostInputDataMessage);
            }

            var loggedUserId = this.User.Identity.GetUserId();

            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            var pigeonToAdd = new Pigeon
            {
                Title = inputPigeon.Title,
                Content = inputPigeon.Content,
                Author = this.Data.Users.Search(u => u.Id == loggedUserId).FirstOrDefault(),
                AuthorId = loggedUserId,
                CreatedOn = DateTime.Now,
                Comments = new HashSet<Comment>(),
                Votes = new HashSet<PigeonVote>()
            };

            if (inputPigeon.PhotoData != null)
            {
                var photo = new Photo { Base64Data = inputPigeon.PhotoData };

                this.Data.Photos.Add(photo);
                pigeonToAdd.Photo = photo;
            }

            this.Data.Pigeons.Add(pigeonToAdd);
            this.Data.SaveChanges();

            var pigeonViewModel = PigeonViewModel.CreateSingle(pigeonToAdd);

            return this.Ok(pigeonViewModel);
        }
Ejemplo n.º 2
0
        public IHttpActionResult EditPigeon(int id, PigeonBindingModel updatedPigeon)
        {
            if (updatedPigeon == null)
            {
                return this.BadRequest(InvalidPigeonUpdateInputDataMessage);
            }

            var loggedUserId = this.User.Identity.GetUserId();
            var pigeonToUpdate = this.Data.Pigeons.GetById(id);

            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            if (pigeonToUpdate == null)
            {
                return this.NotFound();
            }

            if (pigeonToUpdate.Author.Id != loggedUserId)
            {
                return this.Unauthorized();
            }

            pigeonToUpdate.Content = updatedPigeon.Content;

            this.Data.Pigeons.Update(pigeonToUpdate);
            this.Data.SaveChanges();

            var pigeonViewModel = new { pigeonToUpdate.Content };

            return this.Ok(pigeonViewModel);
        }