diff --git a/_cookbook/code/mysql-db/app.cr b/_cookbook/code/mysql-db/app.cr new file mode 100644 index 0000000..65dac07 --- /dev/null +++ b/_cookbook/code/mysql-db/app.cr @@ -0,0 +1,68 @@ +require "kemal" +require "db" +require "mysql" + +# Initialize a single DB connection +DB_URL = "mysql://root:password@localhost:3306/mydb" +DB = DB.open(DB_URL) + +# Example User model +class User + property id : Int32 + property name : String + property email : String + + def initialize(@id, @name, @email) + end +end + +# List all users +get "/users" do |env| + # Initialize empty array to store User objects + users = [] of User + + # Query database for all users + DB.query "SELECT * FROM users" do |rs| + # For each row in result set + rs.each do + # Create new User object from row data and add to array + users << User.new( + id: rs.read(Int32), # Read id column as Integer + name: rs.read(String), # Read name column as String + email: rs.read(String) # Read email column as String + ) + end + end + + # Return users array as JSON response + users.to_json +end + +# Create a new user +post "/users" do |env| + name = env.params.json["name"].as(String) + email = env.params.json["email"].as(String) + + user_id = DB.exec "INSERT INTO users (name, email) VALUES (?, ?) RETURNING id", name, email do |rs| + rs.each { return rs.read(Int32) } + end + + {message: "User created with id: #{user_id}"}.to_json +end + +# Delete a user +delete "/users/:id" do |env| + id = env.params.url["id"].to_i + + # Delete user and check if any rows were affected + result = DB.exec "DELETE FROM users WHERE id = ?", id + + if result.rows_affected > 0 + {message: "User deleted successfully"}.to_json + else + env.response.status_code = 404 + {message: "User not found"}.to_json + end +end + +Kemal.run diff --git a/_cookbook/mysql-db.md b/_cookbook/mysql-db.md new file mode 100644 index 0000000..7dd342b --- /dev/null +++ b/_cookbook/mysql-db.md @@ -0,0 +1,18 @@ +--- +layout: doc +title: MySQL DB Recipe +--- + +Add the `mysql` shard to your `shard.yml` file. + +```yaml +dependencies: + mysql: + github: crystal-lang/crystal-mysql +``` + +```ruby +{% include_relative code/mysql-db/app.cr %} +``` + +[Source Code](https://github.com/kemalcr/kemalcr.com/tree/master/_cookbook/code/mysql-db) \ No newline at end of file