Skip to content

Instantly share code, notes, and snippets.

@sean3z
sean3z / index.html
Created March 18, 2018 02:43
Websocket example (Native websocket)
<div id="content"></div>
<script type="text/javascript">
var content = document.getElementById('content');
var socket = new WebSocket('ws://localhost:1337');
socket.onopen = function () {
socket.send('hello from the client');
};
socket.onmessage = function (message) {
@sean3z
sean3z / down.sql
Last active March 18, 2018 03:53
Creating our Heroes schema
-- This file should undo anything in `up.sql`
DROP TABLE heroes;
@sean3z
sean3z / hero.rs
Last active March 18, 2018 03:57
bind our struct to our table
use diesel;
use diesel::prelude::*;
use diesel::mysql::MysqlConnection;
use schema::heroes;
#[table_name = "heroes"]
#[derive(Serialize, Deserialize, Queryable, Insertable)]
pub struct Hero {
pub id: Option<i32>,
pub name: String,
@sean3z
sean3z / schema.rs
Created March 18, 2018 04:51
Diesel schema macro - id Nullable
table! {
heroes {
id -> Nullable<Integer>,
name -> Varchar,
identity -> Varchar,
hometown -> Varchar,
age -> Integer,
}
}
@sean3z
sean3z / main.rs
Last active March 18, 2018 15:37
Rocket Getting Started snippet
#![feature(plugin)]
#![plugin(rocket_codegen)]
extern crate rocket;
#[get("/<name>/<age>")]
fn hello(name: String, age: u8) -> String {
format!("Hello, {} year old named {}!", age, name)
}
@sean3z
sean3z / hero.rs
Last active April 3, 2018 17:05
Hero implementation
impl Hero {
pub fn create(hero: Hero, connection: &MysqlConnection) -> Hero {
diesel::insert_into(heroes::table)
.values(&hero)
.execute(connection)
.expect("Error creating new hero");
heroes::table.order(heroes::id.desc()).first(connection).unwrap()
}
@sean3z
sean3z / main.rs
Last active April 6, 2018 02:26
Updating our routes to use real CRUD operations
#[post("/", data = "<hero>")]
fn create(hero: Json<Hero>, connection: db::Connection) -> Json<Hero> {
let insert = Hero { id: None, ..hero.into_inner() };
Json(Hero::create(insert, &connection))
}
#[get("/")]
fn read(connection: db::Connection) -> Json<Value> {
Json(json!(Hero::read(&connection)))
}
@sean3z
sean3z / index.html
Created April 21, 2018 15:24
Pagination example
<div>
<span>1</span>
<span><a href="?p=1">2</a></span>
<span><a href="?p=2">3</a></span>
<span><a href="?p=3">4</a></span>
<span><a href="?p=4">5</a></span>
...
<span><a href="?p=1">next &rsaquo;</a></span>
<span><a href="?p=9">last &raquo;</a></span>
</div>
@sean3z
sean3z / import.php
Created April 21, 2018 15:28
Drupal 6 migration example
<?php
function import_nodes() {
db_query('DELETE FROM {node} WHERE `nid` > 1');
db_query('ALTER TABLE {node} AUTO_INCREMENT = 2');
db_query('DELETE FROM {node_revision} WHERE `nid` > 1');
db_query('ALTER TABLE {node_revision} AUTO_INCREMENT = 2');
db_query('DELETE FROM {field_revision_body} WHERE `entity_id` > 1');
db_query('DELETE FROM {field_data_body} WHERE `entity_id` > 1');
db_query('DELETE FROM {node_comment_statistics} WHERE `nid` > 1');
db_query('TRUNCATE TABLE {url_alias}');
@sean3z
sean3z / config.php
Created April 21, 2018 15:29
drupal 6 migration example
$databases['drupal6']['default'] = array(
'driver' => 'mysql',
'database' => 'database',
'username' => 'username',
'password' => 'password',
'host' => 'hostname',
'prefix' => '',
);