Exemplo n.º 1
0
        public async Task <HttpResponseMessage> PostComment()
        {
            try
            {
                if (!Request.Content.IsMimeMultipartContent())
                {
                    throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
                }
                string root     = HttpContext.Current.Server.MapPath("~/" + UPLOAD_DIR);
                var    provider = new MultipartFormDataStreamProvider(root);
                var    forumId  = string.Empty;
                var    userId   = string.Empty;
                var    comment  = string.Empty;
                await Request.Content.ReadAsMultipartAsync(provider);

                CourseDetailProxy course = new CourseDetailProxy();
                foreach (var key in provider.FormData)
                {
                    if (key.Equals("forumId"))
                    {
                        forumId = JsonConvert.DeserializeObject <string>(provider.FormData[key.ToString()]);
                    }
                    if (key.Equals("userId"))
                    {
                        userId = JsonConvert.DeserializeObject <string>(provider.FormData[key.ToString()]);
                    }
                    if (key.Equals("comment"))
                    {
                        comment = JsonConvert.DeserializeObject <string>(provider.FormData[key.ToString()]);
                    }
                }
                var forum = this._forumRepository.FindById(forumId);
                this._commentService.Post(new Comment
                {
                    Description = comment,
                    ForumId     = forumId,
                    CreatedDate = this._datetimeRepository.Now()
                }, userId);
                return(Request.CreateResponse(HttpStatusCode.OK));
            }
            catch (System.Exception e)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e));
            }
        }
Exemplo n.º 2
0
 public static Course ToEntity(this CourseDetailProxy course)
 {
     return(new Data.Models.Course
     {
         Id = course.Id,
         ImageUrl = course.ImageUrl,
         Language = course.Language,
         Name_EN = course.Name_EN,
         Name_TH = course.Name_TH,
         Price = course.Price,
         FullDescription = course.FullDescription,
         BigImageUrl = course.BigImageUrl,
         CategoryId = course.CategoryId,
         CreatedDate = course.CreatedDate,
         CreatedUser = course.CreatedUserId,
         UpdatedDate = course.UpdateDate
     });
 }
Exemplo n.º 3
0
        public async Task <HttpResponseMessage> UploadPhoto()
        {
            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }
            string root         = HttpContext.Current.Server.MapPath("~/" + UPLOAD_DIR);
            string uploadPath   = string.Empty;
            string courseId     = string.Empty;
            string albumId      = string.Empty;
            string userId       = string.Empty;
            string title        = string.Empty;
            var    dummyAlbumId = Guid.NewGuid().ToString();
            var    provider     = new MultipartFormDataStreamProvider(root);

            try
            {
                await Request.Content.ReadAsMultipartAsync(provider);

                CourseDetailProxy course = new CourseDetailProxy();
                foreach (var key in provider.FormData)
                {
                    if (key.Equals("course"))
                    {
                        courseId = JsonConvert.DeserializeObject <string>(provider.FormData[key.ToString()]);
                    }
                    if (key.Equals("album"))
                    {
                        albumId = JsonConvert.DeserializeObject <string>(provider.FormData[key.ToString()]);
                    }
                    if (key.Equals("user"))
                    {
                        userId = JsonConvert.DeserializeObject <string>(provider.FormData[key.ToString()]);
                    }
                    if (key.Equals("name"))
                    {
                        title = JsonConvert.DeserializeObject <string>(provider.FormData[key.ToString()]);
                    }
                }
                albumId    = String.IsNullOrEmpty(albumId) ? dummyAlbumId : albumId;
                uploadPath = root + albumId + @"\";
                if (!Directory.Exists(uploadPath))
                {
                    Directory.CreateDirectory(uploadPath);
                }
                string _newFileName = string.Empty;
                string _path        = string.Empty;
                foreach (MultipartFileData file in provider.FileData)
                {
                    string fileName = file.Headers.ContentDisposition.FileName;
                    if (fileName.StartsWith("\"") && fileName.EndsWith("\""))
                    {
                        fileName = fileName.Trim('"');
                    }
                    if (fileName.Contains(@"/") || fileName.Contains(@"\"))
                    {
                        fileName = Path.GetFileName(fileName);
                    }
                    _newFileName = string.Format("{0}{1}", Guid.NewGuid(), Path.GetExtension(fileName));
                    var moveTo = Path.Combine(uploadPath, _newFileName);
                    if (File.Exists(moveTo))
                    {
                        File.Delete(moveTo);
                    }
                    _path = string.Format("{0}{1}", UPLOAD_DIR + albumId + "/", _newFileName);
                    File.Move(file.LocalFileName, moveTo);
                }
                var photo = new PhotoProxy
                {
                    Name          = title,
                    ImageUrl      = _path,
                    PublishedDate = this._datetimeRepository.Now()
                };
                this._photoAlbumService.AddNewPhoto(courseId, albumId, userId, photo.ToEntity());
                return(Request.CreateResponse(HttpStatusCode.OK));
            }
            catch (System.Exception e)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e));
            }
        }
Exemplo n.º 4
0
        public async Task <HttpResponseMessage> PostFormData()
        {
            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }
            var    GuID     = Guid.NewGuid().ToString();
            string root     = HttpContext.Current.Server.MapPath("~/" + UPLOAD_DIR);
            var    provider = new MultipartFormDataStreamProvider(root);

            try
            {
                await Request.Content.ReadAsMultipartAsync(provider);

                CourseDetailProxy course = new CourseDetailProxy();
                foreach (var key in provider.FormData)
                {
                    if (key.Equals("course"))
                    {
                        var json = provider.FormData[key.ToString()];
                        course = JsonConvert.DeserializeObject <CourseDetailProxy>(json);
                    }
                }
                string _newFileName = string.Empty;
                string _path        = string.Empty;
                foreach (MultipartFileData file in provider.FileData)
                {
                    string fileName = file.Headers.ContentDisposition.FileName;
                    if (fileName.StartsWith("\"") && fileName.EndsWith("\""))
                    {
                        fileName = fileName.Trim('"');
                    }
                    if (fileName.Contains(@"/") || fileName.Contains(@"\"))
                    {
                        fileName = Path.GetFileName(fileName);
                    }
                    _path = string.Format("{0}{1}", UPLOAD_DIR, fileName);
                    var moveTo = Path.Combine(root, fileName);
                    if (File.Exists(moveTo))
                    {
                        File.Delete(moveTo);
                    }
                    _path = string.Format("{0}{1}", UPLOAD_DIR, fileName);
                    File.Move(file.LocalFileName, moveTo);
                    course.ImageUrl = string.Format("{0}?{1}", _path, this._datetimeRepository.Now());
                }
                if (string.IsNullOrEmpty(course.Id))
                {
                    course.Id          = GuID;
                    course.CreatedDate = this._datetimeRepository.Now();
                    this._courseService.Add(course.ToEntity(), course.CreatedUserId);
                }
                else
                {
                    var c = this._courseRepository.FindById(course.Id);
                    c.ImageUrl        = course.ImageUrl;
                    c.Language        = course.Language;
                    c.Name_EN         = course.Name_EN;
                    c.Name_TH         = course.Name_TH;
                    c.Price           = course.Price;
                    c.FullDescription = course.FullDescription;
                    c.BigImageUrl     = course.BigImageUrl;
                    c.CategoryId      = course.CategoryId;
                    c.UpdatedDate     = this._datetimeRepository.Now();
                    c.UpdatedUser     = course.CreatedUserId;
                    this._courseService.Update(c);
                }
                return(Request.CreateResponse(HttpStatusCode.OK));
            }
            catch (System.Exception e)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e));
            }
        }