Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: version 1 #6

Merged
merged 2 commits into from
Sep 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .adonisrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"typescript": true,
"commands": [
"./commands",
"@adonisjs/core/build/commands",
"@adonisjs/lucid/build/commands"
],
"exceptionHandlerNamespace": "App/Exceptions/Handler",
"aliases": {
"App": "app",
"Contracts": "contracts",
"Config": "config",
"Database": "database"
},
"preloads": [
"./start/routes",
"./start/kernel"
],
"providers": [
"./providers/AppProvider",
"@adonisjs/core",
"@adonisjs/session",
"@adonisjs/view",
"@adonisjs/lucid"
],
"metaFiles": [
".env",
".adonisrc.json",
"resources/views/**/*.edge",
"public/**"
]
}
14 changes: 14 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.json]
insert_final_newline = ignore

[*.md]
trim_trailing_whitespace = false
11 changes: 11 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
PORT=
HOST=
NODE_ENV=
APP_KEY=
SESSION_DRIVER=
DB_CONNECTION=
DB_HOST=
DB_USER=
DB_PASSWORD=
DB_NAME=
DB_PORT=
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build
5 changes: 5 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": [
"plugin:adonis/typescriptApp"
]
}
107 changes: 5 additions & 102 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,104 +1,7 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
node_modules
build
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.vscode
.DS_STORE
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache

# Next.js build output
.next

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and *not* Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port
tmp
19 changes: 19 additions & 0 deletions ace
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
|--------------------------------------------------------------------------
| Ace Commands
|--------------------------------------------------------------------------
|
| This file is the entry point for running ace commands. For typescript
| projects, the ace commands will fallback to the compiled code and
| hence this file has to be executable by node directly.
|
*/

require('reflect-metadata')
require('source-map-support').install({ handleUncaughtExceptions: false })

const { Ignitor } = require('@adonisjs/core/build/src/Ignitor')
new Ignitor(__dirname)
.ace()
.handle(process.argv.slice(2))
.catch(console.error)
102 changes: 102 additions & 0 deletions app/Controllers/Http/ProductsController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { HttpContextContract } from "@ioc:Adonis/Core/HttpContext";
import { schema } from "@ioc:Adonis/Core/Validator";

import Product from "App/Models/Product";

export default class ProductsController {
public async index({ view }: HttpContextContract) {
const products = await Product.all();

return view.render("product/index", {
products,
});
}

public async create({ view }: HttpContextContract) {
return view.render("product/create");
}

public async store({ request, session, response }: HttpContextContract) {
const productSchema = schema.create({
name: schema.string(),
description: schema.string(),
maker: schema.string(),
});

const data = await request.validate({
schema: productSchema,
messages: {
"name.required": "Please enter the product name",
"description.required": "Please enter the product description",
"maker.required": "Please enter the product maker",
},
cacheKey: request.url(),
});

await Product.create(data);

session.flash("success", "Product created successfully");
response.redirect("back");
}

public async show({ params: { id }, view }: HttpContextContract) {
const product = await Product.findOrFail(id);

return view.render("product/show", {
product,
});
}

public async edit({ params: { id }, view }: HttpContextContract) {
const product = await Product.findOrFail(id);

return view.render("product/edit", {
product,
});
}

public async update({
request,
session,
response,
params: { id },
}: HttpContextContract) {
const productSchema = schema.create({
name: schema.string(),
description: schema.string(),
maker: schema.string(),
});

const data = await request.validate({
schema: productSchema,
messages: {
"name.required": "Please enter the product name",
"description.required": "Please enter the product description",
"maker.required": "Please enter the product maker",
},
cacheKey: request.url(),
});

const product = await Product.findOrFail(id);

product.merge(data);

await product.save();

await session.flash("success", "Product updated successfully");
response.redirect("back");
}

public async destroy({
params: { id },
session,
response,
}: HttpContextContract) {
const product = await Product.findOrFail(id);

await product.delete();

await session.flash("success", "Product deleted successfully");
response.redirect("back");
}
}
28 changes: 28 additions & 0 deletions app/Exceptions/Handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
|--------------------------------------------------------------------------
| Http Exception Handler
|--------------------------------------------------------------------------
|
| AdonisJs will forward all exceptions occurred during an HTTP request to
| the following class. You can learn more about exception handling by
| reading docs.
|
| The exception handler extends a base `HttpExceptionHandler` which is not
| mandatory, however it can do lot of heavy lifting to handle the errors
| properly.
|
*/

import Logger from '@ioc:Adonis/Core/Logger'
import HttpExceptionHandler from '@ioc:Adonis/Core/HttpExceptionHandler'

export default class ExceptionHandler extends HttpExceptionHandler {
protected statusPages = {
'404': 'errors.not-found',
'500..599': 'errors.server-error',
}

constructor () {
super(Logger)
}
}
22 changes: 22 additions & 0 deletions app/Models/Product.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { DateTime } from "luxon";
import { BaseModel, column } from "@ioc:Adonis/Lucid/Orm";

export default class Product extends BaseModel {
@column({ isPrimary: true })
public id: number;

@column()
public name: string;

@column()
public description: string;

@column()
public maker: string;

@column.dateTime({ autoCreate: true })
public createdAt: DateTime;

@column.dateTime({ autoCreate: true, autoUpdate: true })
public updatedAt: DateTime;
}
19 changes: 19 additions & 0 deletions commands/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { listDirectoryFiles } from '@adonisjs/ace'
import Application from '@ioc:Adonis/Core/Application'

/*
|--------------------------------------------------------------------------
| Exporting an array of commands
|--------------------------------------------------------------------------
|
| Instead of manually exporting each file from this directory, we use the
| helper `listDirectoryFiles` to recursively collect and export an array
| of filenames.
|
| Couple of things to note:
|
| 1. The file path must be relative from the project root and not this directory.
| 2. We must ignore this file.
|
*/
export default listDirectoryFiles(__dirname, Application.appRoot, ['./commands/index.js'])
Loading