示例#1
0
 public HttpResponseMessage UploadAssignment([FromBody] JObject assignmentInfo)
 {
     try
     {
         string signature = HttpUtil.GetAuthorization(Request);
         if (signature == null || !redis.IsSet(signature))
         {
             return(new Response(2001, "未登录账户").Convert());
         }
         bool isLogin = redis.IsSet(signature);
         if (!isLogin)
         {
             return(new Response(2001, "未登录账户").Convert());
         }
         string     id                     = redis.Get <string>(signature);
         var        jsonParams             = HttpUtil.Deserialize(assignmentInfo);
         int        expID                  = jsonParams.exp_id;
         string     file                   = jsonParams.file;
         Experiment experiment             = ExperimentDao.GetExperimentById(expID);
         List <Course_student_mapping> csm = CourseDao.GetMapByStudentId(id).Where(m => m.course_id == experiment.course_id).ToList();
         if (csm.Count > 0)
         {
             Assignment assignment = new Assignment
             {
                 experiment_id = experiment.id,
                 student_id    = id,
                 submit_time   = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"),
                 is_standard   = 0,
                 file          = file
             };
             Assignment oldAssignment = AssignmentDao.GetAssignmentsByStuIdAndExpId(id, expID);
             if (oldAssignment != null)
             {
                 LogUtil.Log(Request, "重新提交作业", expID.ToString(), id, 1, "", "", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
                 AssignmentDao.DeleteAssignment(oldAssignment.id);
                 AssignmentDao.AddAssignment(assignment);
                 return(new Response(1001, "重新提交作业成功").Convert());
             }
             else
             {
                 LogUtil.Log(Request, "提交作业", expID.ToString(), id, 1, "", "", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
                 AssignmentDao.AddAssignment(assignment);
                 return(new Response(1001, "提交作业成功").Convert());
             }
         }
         return(new Response(2002).Convert());
     }
     catch (Exception e)
     {
         ErrorLogUtil.WriteLogToFile(e, Request);
         return(Response.Error());
     }
 }
示例#2
0
        public HttpResponseMessage UploadAssignment()
        {
            try
            {
                string signature = HttpUtil.GetAuthorization(Request);
                if (signature == null || !redis.IsSet(signature))
                {
                    return(new Response(2001, "未登录账户").Convert());
                }
                bool login = redis.IsSet(signature);
                if (!login)
                {
                    return(new Response(2001, "未登录账户").Convert());
                }
                string          id          = redis.Get <string>(signature);
                HttpContextBase context     = (HttpContextBase)Request.Properties["MS_HttpContext"];
                HttpRequestBase httpRequest = context.Request;

                int expId = int.Parse(httpRequest["expId"]);


                Experiment exp = ExperimentDao.GetExperimentById(expId);
                if (!HttpUtil.IsTimeLater(exp.deadline))
                {
                    return(new Response(2002, "已过提交截止时间").Convert());
                }
                if (exp == null)
                {
                    return(new Response(3001).Convert());
                }
                Course_student_mapping csm = CourseDao.GetMapByStudentId(id).Where(m => m.course_id == exp.course_id).First();
                if (csm == null)
                {
                    return(new Response(2002).Convert());
                }

                string fileCode = null;
                if (httpRequest.Files.Count > 0)
                {
                    string path = httpRequest.MapPath(@"~/Files/Assignment/" + expId + "/");
                    if (!System.IO.Directory.Exists(path))
                    {
                        System.IO.Directory.CreateDirectory(path);
                    }
                    var uploadedFiles = httpRequest.Files;
                    if (uploadedFiles.Count != 1)
                    {
                        return(new Response(2002, "仅支持单个文件").Convert());
                    }
                    var         uploadedFile = uploadedFiles[0];
                    Models.File file         = HttpUtil.UploadFile(uploadedFile, id, "作业", path, Request);
                    AssignmentDao.AddFile(file);
                    fileCode = file.id;
                }
                else if (httpRequest["file"] != null)
                {
                    fileCode = httpRequest["file"].ToString();
                }
                Assignment assignment = AssignmentDao.GetAssignmentsByStuIdAndExpId(id, expId);
                if (assignment == null)
                {
                    assignment = new Assignment
                    {
                        student_id    = id,
                        experiment_id = expId,
                        submit_time   = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"),
                        file          = fileCode,
                        is_standard   = 0
                    };
                    AssignmentDao.AddAssignment(assignment);
                    LogUtil.Log(Request, "提交作业", expId.ToString(), id, 1);
                }
                else
                {
                    string oldFile = assignment.file;
                    assignment.file        = fileCode;
                    assignment.submit_time = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
                    AssignmentDao.ChangeAssignmentInfo(assignment);
                    LogUtil.Log(Request, "重新提交作业", expId.ToString(), id, 1, "", "替换的作业: " + oldFile);
                }

                return(new Response(1001).Convert());
            }
            catch (Exception e)
            {
                ErrorLogUtil.WriteLogToFile(e, Request);
                return(Response.Error());
            }
        }