public _Badge FindByUserId(Guid user_id) { string query = "select id,name,description, created_at from \"badge\" where user_id = @user_id"; Guid id; string name = ""; string description = ""; DateTime createdDate; using (var cmd = new NpgsqlCommand(query, _connection, _transaction)){ cmd.Parameters.AddWithValue("user_id", user_id); using (NpgsqlDataReader reader = cmd.ExecuteReader()){ if (reader.Read()) { id = reader.GetGuid(0); name = reader["name"] as string; description = reader["description"] as string; createdDate = reader["created_at"] as DateTime? ?? throw new Exception("DateTime error"); } else { return(null); } } } _Badge bdg = new _Badge(id, name, description, user_id, createdDate); return(bdg); }
public void Create(_Badge badge) { string query = "INSERT INTO \"badge\" (id,name,description,user_id) VALUES(@id,@name,@description,@user_id)"; using (var cmd = new NpgsqlCommand(query, _connection, _transaction)){ cmd.Parameters.AddWithValue("id", badge.ID); cmd.Parameters.AddWithValue("name", badge.Name); cmd.Parameters.AddWithValue("description", badge.Description); cmd.Parameters.AddWithValue("user_id", badge.User_Id); cmd.ExecuteNonQuery(); } }
public void checkBadge() { _Badge bg = new _Badge(Guid.NewGuid(), "Dominating", "4+ streak", Guid.NewGuid(), DateTime.Now); Assert.Equal("Dominating", bg.Name); }