public HttpResponseMessage postQuestion() { Console.WriteLine("in"); try { var userId = HttpContext.Current.Session["id"]; //var questionId = HttpContext.Current.Request.QueryString["questionId"]; var title = HttpContext.Current.Request.Params["title"]; var content = HttpContext.Current.Request.Params["content"]; DateTime time = DateTime.Now; file_request newRequest = new file_request() { title = (string)title, description = (string)content, post_time = time, status = 1, user_id = (int)userId }; using (FileEntitiesFinal entity = new FileEntitiesFinal()) { entity.file_request.Add(newRequest); entity.SaveChangesAsync(); return(Request.CreateResponse(HttpStatusCode.OK, "Success")); } } catch (Exception e) { return(Request.CreateResponse(HttpStatusCode.InternalServerError, "Fail to post")); } }
public HttpResponseMessage postAnswer(int questionId) { try { var userId = HttpContext.Current.Session["id"]; //var questionId = HttpContext.Current.Request.QueryString["questionId"]; //var title = HttpContext.Current.Request.Params["title"]; var content = HttpContext.Current.Request.Params["content"]; DateTime time = DateTime.Now; answer newAnswer = new answer { content = (string)content, user_id = (int)userId, file_request_id = questionId, answer_time = time }; using (FileEntitiesFinal entity = new FileEntitiesFinal()) { file_request request = entity.file_request.Where(r => r.id == questionId).FirstOrDefault(); if (request == null) { return(Request.CreateResponse(HttpStatusCode.NotFound, "No such question")); } entity.answer.Add(newAnswer); entity.SaveChangesAsync(); return(Request.CreateResponse(HttpStatusCode.OK, "Success")); } } catch (Exception e) { return(Request.CreateResponse(HttpStatusCode.InternalServerError, "Fail to post")); } }
// store the file into the database public async Task StoreFileToDB(file file) { // using transaction scope using (var scope = new TransactionScope()) { using (FileEntitiesFinal entity = new FileEntitiesFinal()) { entity.file.Add(file); await entity.SaveChangesAsync(); scope.Complete(); } } }
private async void addUpFileNumbs(int userId) { using (TransactionScope scope = new TransactionScope()) { using (FileEntitiesFinal entity = new FileEntitiesFinal()) { user tempUser = entity.user.Where(u => u.id == userId).FirstOrDefault(); if (tempUser == null) { return; } tempUser.file_nums++; entity.user.Attach(tempUser); entity.Entry(tempUser).State = System.Data.Entity.EntityState.Modified; await entity.SaveChangesAsync(); } } }
//private string GetRandomString(int length) //{ // const string key = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; // if (length < 1) // { // return string.Empty; // } // Random random = new Random(); // byte[] buffer = new byte[8]; // ulong bit = 21; // ulong result = 0; // int index = 0; // StringBuilder stringBuilder = new StringBuilder((length / 5 + 1) * 5); // while (stringBuilder.Length <length) // { // random.NextBytes(buffer); // buffer[5] = buffer[6] = buffer[7] = 0x00; // result = BitConverter.ToUInt64(buffer, 0); // while (result >0 && stringBuilder.Length <length) // { // index = (int)(bit & result); // stringBuilder.Append(key[index]); // result = result >> 5; // } // } // return stringBuilder.ToString(); // } private async void addUpDownloadTimes(int fileId) { using (TransactionScope scope = new TransactionScope()) { using (FileEntitiesFinal entity = new FileEntitiesFinal()) { file tempFile = entity.file.Where(f => f.id == fileId).FirstOrDefault(); if (tempFile == null) { return; } else { tempFile.download_times++; entity.file.Attach(tempFile); entity.Entry(tempFile).State = System.Data.Entity.EntityState.Modified; await entity.SaveChangesAsync(); } } scope.Complete(); } }
public HttpResponseMessage getDownloadCode(int fileId) { using (FileEntitiesFinal entity = new FileEntitiesFinal()) { var userId = HttpContext.Current.Session["id"]; string code = ""; user_share_file user_Share_File = entity.user_share_file.Where(u => u.file_id == fileId && u.user_id == (int)userId).FirstOrDefault(); if (entity.file.Where(f => f.id == fileId).FirstOrDefault() == null) { return(Request.CreateResponse(HttpStatusCode.NotFound, "No such file")); } RandomCodeLib.CodeClass codeClass = new CodeClass(); if (user_Share_File == null) { codeClass.getRandomCode(6, out code); while (isCodeExist(code)) { codeClass.getRandomCode(6, out code); if (code.Length < 6) { return(Request.CreateResponse(HttpStatusCode.InternalServerError, "File to get the code")); } } user_Share_File = new user_share_file { user_id = (int)userId, file_id = fileId, code = code }; entity.user_share_file.Add(user_Share_File); entity.SaveChangesAsync(); } else { code = user_Share_File.code; } return(Request.CreateResponse(HttpStatusCode.OK, code)); } }