コード例 #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));
        }
コード例 #2
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));
        }
コード例 #3
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));
        }
コード例 #4
0
        public HttpResponseMessage PostDeactivateAccount(UserDTO userDTO)
        {
            string email = JwtAuthManager.GetEmailFromRequest(Request);

            if (CheckCredentials(email, userDTO.Password))
            {
                int  userId = JwtAuthManager.GetUserIdFromRequest(Request);
                User user   = db.Users.Find(userId);

                List <Comment> allComments = db.Comments.Where(c => c.UserDetails.UserDetailsId == userId).ToList();
                allComments.ForEach(c => db.Comments.Remove(c));

                int?imageId = Image.ExtractImageId(user.UserDetails.ImageUrl);

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

                db.UserDetails.Remove(user.UserDetails);

                db.Users.Remove(user);

                db.SaveChanges();

                return(Request.CreateResponse(HttpStatusCode.OK));
            }

            return(Request.CreateResponse(HttpStatusCode.Unauthorized, "Профилот не е деактивиран бидејќи лозинката која ја внесовте е погрешна."));
        }
コード例 #5
0
        // DELETE: api/Courses/5
        public IHttpActionResult DeleteCourse(int id)
        {
            Course course = db.Courses.Find(id);

            if (course != null)
            {
                int?oldImageId = Image.ExtractImageId(course.ImageUrl);
                if (oldImageId != null)
                {
                    ImageController.DeleteImage(oldImageId.Value, db);
                }
                db.Courses.Remove(course);
                db.SaveChanges();
            }
            return(Ok());
        }
コード例 #6
0
        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));
        }