예제 #1
0
        public async Task <ServiceResponse <string> > SubmitCommentFile(AddCommentFileDto file)
        {
            ServiceResponse <string> response = new ServiceResponse <string>();
            User user = await _context.Users.Include(u => u.ProjectGroups).FirstOrDefaultAsync(u => u.Id == GetUserId());

            Comment comment = await _context.Comments.Include(u => u.CommentedSubmission).FirstOrDefaultAsync(c => c.Id == file.CommentId);

            if (comment == null)
            {
                response.Data    = "No comment";
                response.Message = "There is no comment under this id";
                response.Success = false;
                return(response);
            }
            if (comment.CommentedUserId != user.Id || file.CommentFile == null)
            {
                response.Data    = "Bad Request";
                response.Message = "There is no file to attach or you are not authorized";
                response.Success = false;
                return(response);
            }
            if (Path.GetExtension(file.CommentFile.FileName) != ".pdf")
            {
                response.Data    = "Bad Request";
                response.Message = "Comments should be pdf";
                response.Success = false;
                return(response);
            }
            var target = Path.Combine(_hostingEnvironment.ContentRootPath, string.Format("{0}/{1}/{2}/{3}/",
                                                                                         "StaticFiles/Feedbacks", comment.CommentedSubmission.CourseId, comment.CommentedSubmissionId, user.Id));

            Directory.CreateDirectory(target);
            if (file.CommentFile.Length <= 0)
            {
                response.Success = false;
            }
            else
            {
                string oldfile = Directory.GetFiles(target).FirstOrDefault();
                if (File.Exists(oldfile))
                {
                    File.Delete(oldfile);
                }
                var filePath = Path.Combine(target, user.Name.Trim().Replace(" ", "_") + "_Feedback.pdf");
                comment.FilePath = filePath;
                comment.FileAttachmentAvailability = true;
                using (var stream = new FileStream(filePath, FileMode.Create))
                {
                    await file.CommentFile.CopyToAsync(stream);
                }
                response.Data     = target;
                response.Message  = "file succesfully saved.";
                comment.CreatedAt = DateTime.Now;
            }
            _context.Comments.Update(comment);
            await _context.SaveChangesAsync();

            return(response);
        }
예제 #2
0
        public async Task <IActionResult> Submit(IFormFile file, int commentId)
        {
            AddCommentFileDto dto = new AddCommentFileDto {
                CommentFile = file, CommentId = commentId
            };
            ServiceResponse <string> response = await _commentService.SubmitCommentFile(dto);

            if (response.Success)
            {
                return(Ok(response));
            }
            return(NotFound(response));
        }