Esempio n. 1
0
        //[Authorize()]
        public HttpResponseMessage Put(int id, [FromBody] PrisInfo prisInfo)
        {
            if (!ModelState.IsValid)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
            }

            if (id != prisInfo.PrisInfoId)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }

            var existingPrisInfo = this._unit.PrisInfo.GetById(id);

            _unit.PrisInfo.Detach(existingPrisInfo);

            this._unit.PrisInfo.Update(prisInfo);

            try
            {
                this._unit.SaveChanges();
                //returner en verdi for å unngå å trigge feil-callbacken
                return(Request.CreateResponse(HttpStatusCode.OK,
                                              "{success: 'true', verb: 'PUT'}"));
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex));
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
            }
        }
Esempio n. 2
0
        //[Authorize]
        public ActionResult SlettBilde(PrisInfo prisinfo)
        {
            if (Session["IsAuthenticated"] == null)
            {
                return(Redirect("/Admin/Index"));
            }
            JsonResult result;
            //  var path = Path.Combine(Server.MapPath(image.ImageFile));
            var path = Path.Combine(
                HttpContext.Server.MapPath(Config.PrisImagesFolderPath), prisinfo.ImageFile);
            var file = new FileInfo(path);

            if (file.Exists)
            {
                try
                {
                    using (StreamWriter sw = file.CreateText()) { }
                    file.Delete();
                    result = this.Json(new
                    {
                        status = "success"
                    });
                }
                catch (Exception e)
                {
                    result = this.Json(new
                    {
                        status     = "error",
                        statusText = "Klarer ikke å slette fil fra folder " + e.Message
                    });
                }
            }
            else
            {
                result = this.Json(new
                {
                    status     = "error",
                    statusText = "Finner ikke fil"
                });
            }
            return(result);
        }