Exemple #1
0
        // POST: api/Courses
        public async Task <IHttpActionResult> PostCourse()
        {
            if (!Request.Content.IsMimeMultipartContent())
            {
                return(StatusCode(HttpStatusCode.UnsupportedMediaType));
            }
            var filesReadToProvider = await Request.Content.ReadAsMultipartAsync();

            var    jsonCourse = await filesReadToProvider.Contents[0].ReadAsStringAsync();
            Course course     = JsonConvert.DeserializeObject <Course>(jsonCourse, new CourseJsonConverter()
            {
                DbContext = db
            });

            if (filesReadToProvider.Contents.Count > 1)
            {
                var imageBytes = await filesReadToProvider.Contents[1].ReadAsByteArrayAsync();
                course.ImageUrl = ImageController.SaveImage(imageBytes, Request, db);
            }

            db.Courses.Add(course);
            db.SaveChanges();

            return(Created(Request.RequestUri.ToString() + "/" + course.Id, course));
        }
Exemple #2
0
        public async Task <IHttpActionResult> PatchUser()
        {
            if (!Request.Content.IsMimeMultipartContent())
            {
                return(StatusCode(HttpStatusCode.UnsupportedMediaType));
            }

            int         userId              = JwtAuthManager.GetUserIdFromRequest(Request);
            UserDetails userDetails         = db.UserDetails.Find(userId);
            var         filesReadToProvider = await Request.Content.ReadAsMultipartAsync();

            var imageBytes = await filesReadToProvider.Contents[0].ReadAsByteArrayAsync();

            int?oldImageId = Image.ExtractImageId(userDetails.ImageUrl);

            if (oldImageId != null)
            {
                ImageController.DeleteImage(oldImageId.Value, db);
            }

            userDetails.ImageUrl = ImageController.SaveImage(imageBytes, Request, db);

            db.SaveChanges();

            return(Ok(userDetails));
        }
Exemple #3
0
        public async Task <IHttpActionResult> PatchCourse(int id)
        {
            if (!CourseExists(id))
            {
                return(BadRequest());
            }
            if (!Request.Content.IsMimeMultipartContent())
            {
                return(StatusCode(HttpStatusCode.UnsupportedMediaType));
            }

            Course course = db.Courses.Find(id);
            var    filesReadToProvider = await Request.Content.ReadAsMultipartAsync();

            var imageBytes = await filesReadToProvider.Contents[0].ReadAsByteArrayAsync();

            int?oldImageId = Image.ExtractImageId(course.ImageUrl);

            if (oldImageId != null)
            {
                ImageController.DeleteImage(oldImageId.Value, db);
            }
            course.ImageUrl = ImageController.SaveImage(imageBytes, Request, db);

            db.SaveChanges();

            return(Ok(course));
        }
        public async Task <IHttpActionResult> PostStaff()
        {
            if (!Request.Content.IsMimeMultipartContent())
            {
                return(StatusCode(HttpStatusCode.UnsupportedMediaType));
            }
            var filesReadToProvider = await Request.Content.ReadAsMultipartAsync();

            var   jsonStaff = await filesReadToProvider.Contents[0].ReadAsStringAsync();
            Staff staff     = JsonConvert.DeserializeObject <Staff>(jsonStaff);

            if (filesReadToProvider.Contents.Count > 1)
            {
                var imageBytes = await filesReadToProvider.Contents[1].ReadAsByteArrayAsync();
                staff.ImageUrl = ImageController.SaveImage(imageBytes, Request, db);
            }

            db.Staffs.Add(staff);
            db.SaveChanges();

            return(Created(Request.RequestUri.ToString() + "/" + staff.Id, staff));
        }