// 发表日志 public void submitDiary(string title, string content) { string username = Session["username"].ToString(); DiaryEntities db = new DiaryEntities(); int user_id = db.Users.Where(p => p.user_name == username) .Select(s => s.user_id).First(); Diary newDiaryRecord = new Diary() { diary_title = title, diary_content = content, diary_subtime = DateTime.Now.ToString("yyyy/MM/dd"), diary_author_id = user_id }; db.Diary.Add(newDiaryRecord); try { db.SaveChanges(); } catch (Exception) { Response.Write("Error"); Response.End(); return; } Response.Write("Success"); Response.End(); }
// 加载日志标题和内容 public void loadDiary(int id) { DiaryEntities db = new DiaryEntities(); var record = db.Diary.Where(p => p.diary_id == id).First(); if (record != null) { string title = record.diary_title; string content = record.diary_content.Replace("\"", "'"); string subtime = record.diary_subtime; StringBuilder jsonBuilder = new StringBuilder(); jsonBuilder.Append("{title:\""); jsonBuilder.Append(title); jsonBuilder.Append("\",content:\""); jsonBuilder.Append(content); jsonBuilder.Append("\",subtime:\""); jsonBuilder.Append(subtime); jsonBuilder.Append("\"}"); Response.ContentType = "text/plain"; Response.Charset = "UTF-8"; Response.Write(jsonBuilder.ToString()); Response.End(); } else { Response.Write("Error"); Response.End(); } }
// 保存日志 public void saveDiary(int id, string title, string content) { DiaryEntities db = new DiaryEntities(); var record = db.Diary.Where(p => p.diary_id == id).First(); if (record != null) { record.diary_title = title; record.diary_content = content; try { db.SaveChanges(); } catch (Exception) { Response.Write("Error"); Response.End(); return; } Response.Write("Success"); Response.End(); } }
// 数据 protected int getRecordsCount() { DiaryEntities db = new DiaryEntities(); string username = Session["username"].ToString(); var records = db.View_Diary.Where(p => p.user_name == username).ToList(); if (records?.Count() >= 0) { return(records.Count()); } return(0); }
// 删除日志 public void deleteDiary(int id) { DiaryEntities db = new DiaryEntities(); Diary delRecord = new Diary() { diary_id = id }; db.Diary.Attach(delRecord); db.Diary.Remove(delRecord); try { db.SaveChanges(); } catch (Exception) { Response.Write("Error"); Response.End(); return; } Response.Write("Success"); Response.End(); }