Beispiel #1
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());
            }
        }