/// <summary> /// 新增或保存诗歌 /// </summary> /// <param name="poem"></param> /// <returns>success or fail</returns> public string SavePoem(Poem poem) { try { using (PoemDbContext db = new PoemDbContext()) { if (poem.PoemID == Guid.Parse("00000000-0000-0000-0000-000000000000"))//无id传进来,是新增 { poem.PoemID = Guid.NewGuid(); db.Poem.Add(poem); } else//保存 { var p = db.Poem.Find(poem.PoemID); p.Title = poem.Title; p.Content = poem.Content; } int res = db.SaveChanges(); return(res > 0 ? "success" : "fail"); } } catch (Exception e) { throw e; } }
/// <summary> /// 通过ID查找单个诗歌 /// </summary> /// <param name="pid"></param> /// <returns></returns> public Poem FindPoemById(string pid) { try { using (PoemDbContext db = new PoemDbContext()) { var poem = db.Poem.Find(Guid.Parse(pid)); return(poem); } } catch (Exception e) { throw e; } }
/// <summary> /// 登录 /// </summary> /// <param name="user">登录的用户</param> /// <returns>用户实体</returns> public User Login(User user) { try { using (PoemDbContext db = new PoemDbContext()) { User u = db.User.Where(o => o.UserName == user.UserName && o.Password == user.Password).FirstOrDefault(); return(u ?? null); } } catch (Exception e) { throw e; } }
/// <summary> /// 根据作者查找诗歌 /// </summary> /// <param name="uid">作者id</param> /// <returns></returns> public List <Poem> FindPoemList(string uid) { try { using (PoemDbContext db = new PoemDbContext()) { Guid AutherId = Guid.Parse(uid); List <Poem> list = db.Poem.Where(o => o.AutherID == AutherId).ToList(); return(list); } } catch (Exception e) { throw e; } }
private static SqliteConnection CreateDatabaseAndGetConnection() { var connection = new SqliteConnection("Data Source=:memory:"); connection.Open(); var options = new DbContextOptionsBuilder <PoemDbContext>() .UseSqlite(connection) .Options; using (var context = new PoemDbContext(options)) { context.GetService <IRelationalDatabaseCreator>().CreateTables(); } return(connection); }
/// <summary> /// 新增用户 /// </summary> /// <param name="user">用户实体</param> /// <returns>用户实体</returns> public User AddUser(User user) { try { using (PoemDbContext db = new PoemDbContext()) { user.UserID = Guid.NewGuid(); db.User.Add(user); int res = db.SaveChanges(); return(res > 0 ? user : null); } } catch (Exception e) { throw e; } }
/// <summary> /// 跟新User /// </summary> /// <param name="user"></param> /// <returns>success or fail</returns> public string UpdateUser(User user) { try { int res = 0; using (PoemDbContext db = new PoemDbContext()) { User u = db.User.Find(user.UserID); if (u != null) { u.UserName = user.UserName; u.Password = user.Password; res = db.SaveChanges(); } return(res > 0 ? "success" : "fail"); } } catch (Exception e) { throw e; } }
/// <summary> /// 保存log /// </summary> /// <param name="title">标题</param> /// <param name="content">exception内容</param> /// <param name="sql"></param> /// <param name="request"></param> public static void InsertPLog(string title, string content, string sql, HttpRequest request) { string url = new StringBuilder() .Append(request.Scheme) .Append("://") .Append(request.Host) .Append(request.PathBase) .Append(request.Path) .Append(request.QueryString).ToString(); using (PoemDbContext db = new PoemDbContext()) { PLog pl = new PLog() { Title = title, Content = content, SQL = sql, URL = url }; db.PLog.Add(pl); db.SaveChanges(); } }
public PoemRepository(PoemDbContext context) { _context = context; _poems = context.Poems; }
public TestDataBuilder(PoemDbContext context) { _context = context; }