public IHttpActionResult Get(string userGuid, int courseId, int blockId) { try { string errorInfo = string.Empty; ProgressEnt ent = (new Progress()).Get(userGuid, courseId, blockId, ref errorInfo); if (ent == null) { if (errorInfo == "Progress not exist") { return(NotFound()); } return(BadRequest(errorInfo)); } return(Ok(ent)); } catch (Exception ex) { _log.Error(ex.ToString()); return(BadRequest(ex.ToString())); } }
public ProgressEnt Get(string userGuid, int courseId, int blockId, ref string errorInfo) { _log.Debug("Get progress for " + userGuid); string sql = string.Format("SELECT update_time, detail_result FROM detail_progress WHERE user_guid='{0}' and course_id={1} and block_id={2}" , userGuid, courseId, blockId); DataSet ds = Query(sql); if (ds == null) { errorInfo = "Failed to query user's data"; return(null); } if (ds.Tables[0].Rows.Count == 0) { errorInfo = "Progress not exist"; return(null); } ProgressEnt ent = new ProgressEnt(); ent.UserGuid = userGuid; ent.CourseId = courseId; ent.BlockId = blockId; ent.UpdateTime = DateTime.Parse(ds.Tables[0].Rows[0][0].ToString()); ent.DetailResult = JsonConvert.DeserializeObject <List <ChunkProgressEntity> >(ds.Tables[0].Rows[0][1].ToString()); return(ent); }
public IHttpActionResult Post() { try { string body = new StreamReader(HttpContext.Current.Request.InputStream).ReadToEnd(); ProgressEnt ent = JsonConvert.DeserializeObject <ProgressEnt>(body); string errorInfo = string.Empty; if ((new Progress()).Post(ent, ref errorInfo)) { return(Ok()); } return(BadRequest(errorInfo)); } catch (Exception ex) { _log.Error(ex.ToString()); return(BadRequest(ex.ToString())); } }
public bool Post(ProgressEnt ent, ref string errorInfo) { _log.Debug("Saving progress for " + ent.UserGuid); List <ChunkProgressEntity> words = ent.DetailResult; ProgressEnt oldEnt = Get(ent.UserGuid, ent.CourseId, ent.BlockId, ref errorInfo); if (oldEnt != null) { words.AddRange(oldEnt.DetailResult.FindAll(r => !words.Exists(w => w.ChunkId == r.ChunkId))); } string sql = string.Empty; if (oldEnt != null) { sql = string.Format("UPDATE detail_progress SET detail_result='{0}' and update_time='{1}' WHERE user_guid='{2}' and course_id={3} and block_id={4}" , JsonConvert.SerializeObject(words), DateTime.UtcNow.ToString("yyyy-MM-ddThh:mm:ssZ"), ent.UserGuid, ent.CourseId, ent.BlockId); } else { sql = string.Format("INSERT INTO detail_progress (user_guid, course_id, block_id, detail_result) VALUES ('{0}', {1}, {2}, '{3}')" , ent.UserGuid, ent.CourseId, ent.BlockId, JsonConvert.SerializeObject(words)); } int cnt = ExecuteSql(sql); if (cnt == 0) { errorInfo = "Failed to insert/update database"; return(false); } return(true); }