mirror of
https://github.com/ArthurDanjou/artdanj-api.git
synced 2026-01-27 10:00:27 +01:00
Working on new features
This commit is contained in:
@@ -1,16 +1,18 @@
|
|||||||
import {HttpContextContract} from "@ioc:Adonis/Core/HttpContext";
|
import {HttpContextContract} from "@ioc:Adonis/Core/HttpContext";
|
||||||
import GuestBookMessage from "../../Models/GuestBookMessage";
|
import GuestBookMessage from "../../Models/GuestBookMessage";
|
||||||
import StoreValidator from "../../Validators/guestbook/StoreValidator";
|
import GuestValidator from "../../Validators/guestbook/GuestValidator";
|
||||||
|
|
||||||
export default class GuestBookController {
|
export default class GuestBookController {
|
||||||
|
|
||||||
public async index () {
|
public async get () {
|
||||||
return GuestBookMessage.query().orderBy('created_at', 'desc')
|
return GuestBookMessage.query().orderBy('created_at', 'desc')
|
||||||
}
|
}
|
||||||
|
|
||||||
public async store ({request}: HttpContextContract) {
|
public async store ({request, auth}: HttpContextContract) {
|
||||||
const data = await request.validate(StoreValidator)
|
if (auth.isLoggedIn) {
|
||||||
return await GuestBookMessage.create(data)
|
const data = await request.validate(GuestValidator)
|
||||||
|
return await GuestBookMessage.create(data)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ export default class LocationsController {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
public async add ({ request, response }: HttpContextContract) {
|
public async store ({ request, response }: HttpContextContract) {
|
||||||
const data = await request.validate(LocationValidator)
|
const data = await request.validate(LocationValidator)
|
||||||
await Location.create(data)
|
await Location.create(data)
|
||||||
return response.status(200).send({
|
return response.status(200).send({
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ export default class ProjectsController {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
public async add ({ request, response}: HttpContextContract) {
|
public async store ({ request, response}: HttpContextContract) {
|
||||||
const data = await request.validate(ProjectValidator)
|
const data = await request.validate(ProjectValidator)
|
||||||
await Project.create(data)
|
await Project.create(data)
|
||||||
return response.status(200).send({
|
return response.status(200).send({
|
||||||
|
|||||||
21
app/Controllers/Http/SubscribersController.ts
Normal file
21
app/Controllers/Http/SubscribersController.ts
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
||||||
|
import SubscriberValidator from "../../Validators/subscriber/SubscriberValidator";
|
||||||
|
import Subscriber from "../../Models/Subscriber";
|
||||||
|
|
||||||
|
export default class SubscribersController {
|
||||||
|
|
||||||
|
public async get ({ response }: HttpContextContract) {
|
||||||
|
return response.status(200).send({
|
||||||
|
count: Subscriber.query().count
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
public async store ({ request, response }: HttpContextContract) {
|
||||||
|
const data = await request.validate(SubscriberValidator)
|
||||||
|
await Subscriber.create(data)
|
||||||
|
return response.status(200).send({
|
||||||
|
message: 'Subscriber successfully registered !'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
import {HttpContextContract} from '@ioc:Adonis/Core/HttpContext'
|
import {HttpContextContract} from '@ioc:Adonis/Core/HttpContext'
|
||||||
import User from "App/Models/User";
|
import User from "App/Models/User";
|
||||||
import StoreValidator from "App/Validators/users/StoreValidator";
|
import UserStoreValidator from "../../../app/Validators/users/UserStoreValidator";
|
||||||
import UpdateValidator from "App/Validators/users/UpdateValidator";
|
import UserUpdateValidator from "../../../app/Validators/users/UserUpdateValidator";
|
||||||
|
|
||||||
export default class UsersController {
|
export default class UsersController {
|
||||||
|
|
||||||
@@ -10,7 +10,7 @@ export default class UsersController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async store ({request}: HttpContextContract) {
|
public async store ({request}: HttpContextContract) {
|
||||||
const data = await request.validate(StoreValidator)
|
const data = await request.validate(UserStoreValidator)
|
||||||
return await User.create(data)
|
return await User.create(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -20,7 +20,7 @@ export default class UsersController {
|
|||||||
|
|
||||||
public async update({ request, params, response }: HttpContextContract) {
|
public async update({ request, params, response }: HttpContextContract) {
|
||||||
const user = await User.findOrFail(params.id)
|
const user = await User.findOrFail(params.id)
|
||||||
const data = await request.validate(UpdateValidator)
|
const data = await request.validate(UserUpdateValidator)
|
||||||
const { email } = data
|
const { email } = data
|
||||||
const user2 = await User.findBy('email', email)
|
const user2 = await User.findBy('email', email)
|
||||||
|
|
||||||
|
|||||||
16
app/Models/Subscriber.ts
Normal file
16
app/Models/Subscriber.ts
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
import { DateTime } from 'luxon'
|
||||||
|
import { BaseModel, column } from '@ioc:Adonis/Lucid/Orm'
|
||||||
|
|
||||||
|
export default class Subscriber extends BaseModel {
|
||||||
|
@column({ isPrimary: true })
|
||||||
|
public id: number
|
||||||
|
|
||||||
|
@column()
|
||||||
|
public email: string
|
||||||
|
|
||||||
|
@column.dateTime({ autoCreate: true })
|
||||||
|
public createdAt: DateTime
|
||||||
|
|
||||||
|
@column.dateTime({ autoCreate: true, autoUpdate: true })
|
||||||
|
public updatedAt: DateTime
|
||||||
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
||||||
import {rules, schema} from '@ioc:Adonis/Core/Validator'
|
import {rules, schema} from '@ioc:Adonis/Core/Validator'
|
||||||
|
|
||||||
export default class StoreValidator {
|
export default class GuestValidator {
|
||||||
constructor (private ctx: HttpContextContract) {
|
constructor (private ctx: HttpContextContract) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
||||||
import {rules, schema} from '@ioc:Adonis/Core/Validator'
|
import {rules, schema} from '@ioc:Adonis/Core/Validator'
|
||||||
|
|
||||||
export default class StoreValidator {
|
export default class SubscriberValidator {
|
||||||
constructor (private ctx: HttpContextContract) {
|
constructor (private ctx: HttpContextContract) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -9,8 +9,7 @@ export default class StoreValidator {
|
|||||||
email: schema.string({ trim: true }, [
|
email: schema.string({ trim: true }, [
|
||||||
rules.email(),
|
rules.email(),
|
||||||
rules.unique({table: 'subscribers', column: 'email'})
|
rules.unique({table: 'subscribers', column: 'email'})
|
||||||
]),
|
])
|
||||||
name: schema.string()
|
|
||||||
})
|
})
|
||||||
|
|
||||||
public cacheKey = this.ctx.routeKey
|
public cacheKey = this.ctx.routeKey
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
|
||||||
import {rules, schema} from '@ioc:Adonis/Core/Validator'
|
|
||||||
|
|
||||||
export default class UpdateValidator {
|
|
||||||
constructor (private ctx: HttpContextContract) {
|
|
||||||
}
|
|
||||||
|
|
||||||
public schema = schema.create({
|
|
||||||
email: schema.string({ trim: true }, [
|
|
||||||
rules.email(),
|
|
||||||
rules.unique({table: 'subscribers', column: 'email'})
|
|
||||||
]),
|
|
||||||
name: schema.string.optional()
|
|
||||||
})
|
|
||||||
|
|
||||||
public cacheKey = this.ctx.routeKey
|
|
||||||
|
|
||||||
public messages = {
|
|
||||||
required: 'Le champ {{field}} doit être valide !'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
||||||
import {rules, schema} from '@ioc:Adonis/Core/Validator'
|
import {rules, schema} from '@ioc:Adonis/Core/Validator'
|
||||||
|
|
||||||
export default class StoreValidator {
|
export default class UserStoreValidator {
|
||||||
constructor (private ctx: HttpContextContract) {
|
constructor (private ctx: HttpContextContract) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
||||||
import {rules, schema} from '@ioc:Adonis/Core/Validator'
|
import {rules, schema} from '@ioc:Adonis/Core/Validator'
|
||||||
|
|
||||||
export default class UpdateValidator {
|
export default class UserUpdateValidator {
|
||||||
constructor (private ctx: HttpContextContract) {
|
constructor (private ctx: HttpContextContract) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -6,7 +6,6 @@ export default class Subscribers extends BaseSchema {
|
|||||||
public async up () {
|
public async up () {
|
||||||
this.schema.createTable(this.tableName, (table) => {
|
this.schema.createTable(this.tableName, (table) => {
|
||||||
table.increments('id').primary()
|
table.increments('id').primary()
|
||||||
table.string('name')
|
|
||||||
table.string('email').notNullable()
|
table.string('email').notNullable()
|
||||||
table.timestamps(true)
|
table.timestamps(true)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,34 +0,0 @@
|
|||||||
import BaseSchema from '@ioc:Adonis/Lucid/Schema'
|
|
||||||
|
|
||||||
export default class UsersSchema extends BaseSchema {
|
|
||||||
protected tableName = 'users'
|
|
||||||
|
|
||||||
public async up () {
|
|
||||||
this.schema.createTable(this.tableName, (table) => {
|
|
||||||
table.increments('id').primary()
|
|
||||||
table.string('email', 255).notNullable()
|
|
||||||
table.string('password', 180).defaultTo(this.randomPassword()).notNullable()
|
|
||||||
table.boolean('is_confirmed').defaultTo(false).notNullable()
|
|
||||||
table.string('remember_me_token').defaultTo(null).nullable()
|
|
||||||
table.string('confirmation_token').defaultTo(null).nullable()
|
|
||||||
table.timestamps(true)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
private randomPassword () {
|
|
||||||
let chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
|
||||||
let password = ''
|
|
||||||
let passwordLength = 12
|
|
||||||
|
|
||||||
for (let i = 0; i < passwordLength; i++) {
|
|
||||||
let rnum = Math.floor(Math.random() * chars.length);
|
|
||||||
password += chars.substring(rnum,rnum+1);
|
|
||||||
}
|
|
||||||
|
|
||||||
return password
|
|
||||||
}
|
|
||||||
|
|
||||||
public async down () {
|
|
||||||
this.schema.dropTable(this.tableName)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
import BaseSchema from '@ioc:Adonis/Lucid/Schema'
|
|
||||||
|
|
||||||
export default class DockerCommands extends BaseSchema {
|
|
||||||
protected tableName = 'docker_commands'
|
|
||||||
|
|
||||||
public async up () {
|
|
||||||
this.schema.createTable(this.tableName, (table) => {
|
|
||||||
table.increments('id')
|
|
||||||
table.bigInteger('commands')
|
|
||||||
table.timestamps(true)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
public async down () {
|
|
||||||
this.schema.dropTable(this.tableName)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
import BaseSchema from '@ioc:Adonis/Lucid/Schema'
|
|
||||||
|
|
||||||
export default class DockerBuilds extends BaseSchema {
|
|
||||||
protected tableName = 'docker_builds'
|
|
||||||
|
|
||||||
public async up () {
|
|
||||||
this.schema.createTable(this.tableName, (table) => {
|
|
||||||
table.increments('id')
|
|
||||||
table.bigInteger('builds')
|
|
||||||
table.timestamps(true)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
public async down () {
|
|
||||||
this.schema.dropTable(this.tableName)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -41,6 +41,7 @@ Route.get('/states', 'StatesController.get')
|
|||||||
Route.get('/projects', 'ProjectsController.get')
|
Route.get('/projects', 'ProjectsController.get')
|
||||||
|
|
||||||
Route.resource('users', 'UsersController').only(['index', 'show'])
|
Route.resource('users', 'UsersController').only(['index', 'show'])
|
||||||
|
|
||||||
Route.group(() => {
|
Route.group(() => {
|
||||||
Route.get('/', 'FileController.index')
|
Route.get('/', 'FileController.index')
|
||||||
Route.get('/:filename', async ({ response, params }) => {
|
Route.get('/:filename', async ({ response, params }) => {
|
||||||
@@ -51,22 +52,27 @@ Route.group(() => {
|
|||||||
Route.group(() => {
|
Route.group(() => {
|
||||||
Route.resource('users', 'UsersController').only(['store', 'update', 'destroy'])
|
Route.resource('users', 'UsersController').only(['store', 'update', 'destroy'])
|
||||||
Route.resource('files', 'FileController').only(['store', 'destroy'])
|
Route.resource('files', 'FileController').only(['store', 'destroy'])
|
||||||
Route.post('/locations', 'LocationsController.add')
|
Route.post('/locations', 'LocationsController.store')
|
||||||
Route.post('/projects', 'ProjectsController.add')
|
Route.post('/projects', 'ProjectsController.store')
|
||||||
|
|
||||||
Route.resource('guestbook', 'GuestBookController').only(['index', 'show'])
|
Route.resource('guestbook', 'GuestBookController').only(['index', 'show'])
|
||||||
}).middleware('auth:web')
|
}).middleware('auth:web')
|
||||||
|
|
||||||
Route.group(() => {
|
Route.group(() => {
|
||||||
// ArtAPI
|
|
||||||
Route.post('form', 'FormsController.send')
|
Route.post('form', 'FormsController.send')
|
||||||
// ArtSite
|
|
||||||
Route.group(() => {
|
Route.group(() => {
|
||||||
Route.get('/:slug', 'PostsController.getLikes')
|
Route.get('/:slug', 'PostsController.getLikes')
|
||||||
Route.post('/:slug/like', 'PostsController.like')
|
Route.post('/:slug/like', 'PostsController.like')
|
||||||
Route.post('/:slug/unlike', 'PostsController.unlike')
|
Route.post('/:slug/unlike', 'PostsController.unlike')
|
||||||
}).prefix('posts')
|
}).prefix('posts')
|
||||||
}).middleware('auth:api')
|
|
||||||
|
Route.get('subscribers', 'SubscribersController.get')
|
||||||
|
Route.post('subscribers', 'SubscribersController.store')
|
||||||
|
|
||||||
|
Route.get('guestbook', 'GuestBookController.get')
|
||||||
|
Route.post('guestbook', 'GuestBookController.store')
|
||||||
|
})//.middleware('auth:api')
|
||||||
|
|
||||||
Route.group(() => {
|
Route.group(() => {
|
||||||
Route.get('/me', 'AuthController.user').middleware('auth')
|
Route.get('/me', 'AuthController.user').middleware('auth')
|
||||||
|
|||||||
Reference in New Issue
Block a user