Ejemplo n.º 1
0
        public IHttpActionResult FishEdit(Fish model, HttpPostedFileBase imageFile)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var fish = FishDb.Fishes.Where(x => x.Id == model.Id).FirstOrDefault();

            if (fish == null)
            {
                return(NotFound());
            }
            if (!AuthorizeCurrentUser(fish))
            {
                return(BadRequest());
            }

            //modify Title and Description
            fish.Title       = (model.Title != null && model.Title.ToLower() != "null" ? model.Title : "");
            fish.Description = (model.Description != null && model.Description.ToLower() != "null" ? model.Description : "");

            //resize image
            UpdateFishImage(imageFile, fish);

            FishDb.Entry(fish).State = EntityState.Modified;
            FishDb.SaveChanges();

            return(StatusCode(HttpStatusCode.NoContent));
        }
Ejemplo n.º 2
0
        public IHttpActionResult DeleteFish(int id)
        {
            Fish fish = FishDb.Fishes.Find(id);

            if (fish == null)
            {
                return(NotFound());
            }
            if (!AuthorizeCurrentUser(fish))
            {
                return(BadRequest());
            }

            ControllerHelpers.DeleteBlobs(fish, imagesBlobContainer);
            FishDb.Fishes.Remove(fish);
            FishDb.SaveChanges();

            return(Ok(fish));
        }
Ejemplo n.º 3
0
        public IHttpActionResult FishCreate(Fish model, HttpPostedFileBase imageFile)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            //modify Title and Description
            var fish = new Fish
            {
                Title         = (model.Title != null && model.Title.ToLower() != "null" ? model.Title : ""),
                Description   = (model.Description != null && model.Description.ToLower() != "null" ? model.Description : ""),
                CreatedUserId = User.Identity.GetUserId(),
                CreatedDate   = DateTime.Now
            };

            //resize new image
            UpdateFishImage(imageFile, fish);

            FishDb.Fishes.Add(model);
            FishDb.SaveChanges();

            return(CreatedAtRoute("DefaultApi", new { id = model.Id }, model));
        }