コード例 #1
0
        public IHttpActionResult GetHinhAnhTram(int id)
        {
            HinhAnhTram hinhAnhTram = db.HinhAnhTrams.Find(id);

            if (hinhAnhTram == null)
            {
                return(NotFound());
            }
            var tmp = db.Trams.SingleOrDefault(u => u.IDTram == hinhAnhTram.IDTram);

            if (!isAccess(tmp.IDQuanLy))
            {
                return(StatusCode(HttpStatusCode.Unauthorized));
            }
            return(Ok(hinhAnhTram));
        }
コード例 #2
0
        public IHttpActionResult PutHinhAnhTram(int id, HinhAnhTram hinhAnhTram)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != hinhAnhTram.IDHinhAnh)
            {
                return(BadRequest());
            }
            HinhAnhTram hinhAnhTramBefore = db.HinhAnhTrams.Find(id);

            if (hinhAnhTramBefore == null)
            {
                return(NotFound());
            }
            var tmp = db.Trams.SingleOrDefault(u => u.IDTram == hinhAnhTramBefore.IDTram);

            if (!isAccess(tmp.IDQuanLy))
            {
                return(StatusCode(HttpStatusCode.Unauthorized));
            }

            hinhAnhTramBefore.Ten = hinhAnhTram.Ten;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!HinhAnhTramExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
コード例 #3
0
        public IHttpActionResult PostHinhAnhTram(HinhAnhTram hinhAnhTram)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var tmp = db.Trams.SingleOrDefault(u => u.IDTram == hinhAnhTram.IDTram);

            if (tmp == null)
            {
                return(NotFound());
            }
            if (!isAccess(tmp.IDQuanLy))
            {
                return(StatusCode(HttpStatusCode.Unauthorized));
            }

            db.HinhAnhTrams.Add(hinhAnhTram);
            db.SaveChanges();

            return(CreatedAtRoute("DefaultApi", new { id = hinhAnhTram.IDHinhAnh }, hinhAnhTram));
        }
コード例 #4
0
        public IHttpActionResult PostHinhAnhTram(int idTram)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var tmp = db.Trams.SingleOrDefault(u => u.IDTram == idTram);

            if (tmp == null)
            {
                return(NotFound());
            }
            //if (!isAccess(tmp.IDQuanLy))
            //    return StatusCode(HttpStatusCode.Unauthorized);

            try
            {
                var httpRequest = HttpContext.Current.Request;

                foreach (string file in httpRequest.Files)
                {
                    HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created);

                    var postedFile = httpRequest.Files[file];
                    var ex         = RandomString(10);

                    if (postedFile != null && postedFile.ContentLength > 0)
                    {
                        //var tmpHinhAnhTram = db.HinhAnhTrams.SingleOrDefault(u => u.Ten == postedFile.FileName);
                        //if (tmpHinhAnhTram != null)
                        //{
                        //    return StatusCode(HttpStatusCode.BadRequest);
                        //}
                        int MaxContentLength = 1024 * 1024 * 10; //Size = 10 MB

                        IList <string> AllowedFileExtensions = new List <string> {
                            ".jpg", ".gif", ".png"
                        };
                        var ext       = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf('.'));
                        var extension = ext.ToLower();
                        if (!AllowedFileExtensions.Contains(extension))
                        {
                            return(StatusCode(HttpStatusCode.BadRequest));
                        }
                        else if (postedFile.ContentLength > MaxContentLength)
                        {
                            return(StatusCode(HttpStatusCode.BadRequest));
                        }
                        else
                        {
                            var filePath = HttpContext.Current.Server.MapPath("~/image/" + ex + postedFile.FileName);
                            postedFile.SaveAs(filePath);
                        }
                    }
                    db.HinhAnhTrams.Add(new HinhAnhTram()
                    {
                        IDTram = idTram, Ten = ex + postedFile.FileName
                    });
                    db.SaveChanges();
                    HinhAnhTram hinhAnhTram = db.HinhAnhTrams.SingleOrDefault(u => u.Ten.Equals(ex + postedFile.FileName));
                    return(CreatedAtRoute("DefaultApi", new { id = hinhAnhTram.IDHinhAnh }, hinhAnhTram));
                }
                return(StatusCode(HttpStatusCode.NotFound));
            }
            catch (Exception ex)
            {
                return(StatusCode(HttpStatusCode.NotFound));
            }
        }