mirror of
https://github.com/ArthurDanjou/artdanj-api.git
synced 2026-01-20 23:11:35 +01:00
initital commit
This commit is contained in:
56
app/Controllers/Http/AuthController.ts
Normal file
56
app/Controllers/Http/AuthController.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import {HttpContextContract} from '@ioc:Adonis/Core/HttpContext'
|
||||
import User from "App/Models/User";
|
||||
import AuthValidator from "App/Validators/AuthValidator";
|
||||
|
||||
export default class AuthController {
|
||||
|
||||
public async loginWeb ({request, auth}: HttpContextContract) {
|
||||
const data = await request.validate(AuthValidator)
|
||||
const {email, password, remember_me } = data
|
||||
|
||||
try {
|
||||
await auth.attempt(email, password, remember_me)
|
||||
const user = await User.query()
|
||||
.where('id', auth.user!.id)
|
||||
.firstOrFail()
|
||||
if (!remember_me) {
|
||||
await user.merge({
|
||||
rememberMeToken: ''
|
||||
}).save()
|
||||
}
|
||||
return { user }
|
||||
} catch (error) {
|
||||
if (error.code === 'E_INVALID_AUTH_UID') return { error: "L'utilisateur n'a pas été trouvé" }
|
||||
if (error.code === 'E_INVALID_AUTH_PASSWORD') return { error: "L'identifiant ou le mot de passe est incorrect" }
|
||||
}
|
||||
}
|
||||
|
||||
public async loginApi ({request, auth}: HttpContextContract) {
|
||||
const email = request.input('email')
|
||||
const password = request.input('password')
|
||||
|
||||
const token = await auth.use('api').attempt(email, password, {
|
||||
expiresIn: '10 days'
|
||||
})
|
||||
return token.toJSON()
|
||||
}
|
||||
|
||||
public async logoutWeb ({auth}: HttpContextContract) {
|
||||
await auth.logout()
|
||||
return { message: 'Vous avez été déconnecté' }
|
||||
}
|
||||
|
||||
public async logoutApi ({auth}: HttpContextContract) {
|
||||
await auth.use('api').logout()
|
||||
return { message: 'Vous avez été déconnecté' }
|
||||
}
|
||||
|
||||
public async user ({auth}: HttpContextContract) {
|
||||
await auth.authenticate()
|
||||
const user = await User.query()
|
||||
.where('id', auth.user!.id)
|
||||
.firstOrFail()
|
||||
return { user }
|
||||
}
|
||||
|
||||
}
|
||||
40
app/Controllers/Http/FileController.ts
Normal file
40
app/Controllers/Http/FileController.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
||||
import Application from "@ioc:Adonis/Core/Application";
|
||||
import File from "App/Models/File";
|
||||
|
||||
export default class FileController {
|
||||
|
||||
public async index () {
|
||||
return File.query()
|
||||
}
|
||||
|
||||
public async store ({request}: HttpContextContract) {
|
||||
const file = await request.file('file', {
|
||||
extnames: ['jpg', 'png', 'jpeg']
|
||||
})
|
||||
const label = request.input('label')
|
||||
|
||||
if (!file) {
|
||||
return 'Please upload file'
|
||||
}
|
||||
if (file.hasErrors) {
|
||||
return file.errors
|
||||
}
|
||||
|
||||
await file.move(Application.makePath('storage'), {
|
||||
name: `${label}.${file.extname}`
|
||||
})
|
||||
|
||||
return await File.create({
|
||||
fileName: `${label}.${file.extname}`,
|
||||
label: label
|
||||
})
|
||||
}
|
||||
|
||||
public async destroy({ params }: HttpContextContract) {
|
||||
const file = await File.findOrFail(params.id)
|
||||
await file.delete()
|
||||
return { message: "Le fichier a bien été supprimée" }
|
||||
}
|
||||
|
||||
}
|
||||
74
app/Controllers/Http/PostsController.ts
Normal file
74
app/Controllers/Http/PostsController.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
import {HttpContextContract} from '@ioc:Adonis/Core/HttpContext'
|
||||
import Post from "App/Models/Post";
|
||||
import Redis from "@ioc:Adonis/Addons/Redis";
|
||||
|
||||
export default class PostsController {
|
||||
|
||||
public async getLikes ({params}: HttpContextContract) {
|
||||
let post = await Post.findBy('slug', params.slug)
|
||||
|
||||
if (!post) {
|
||||
post = await Post.create({
|
||||
slug: params.slug,
|
||||
likes: 0
|
||||
})
|
||||
}
|
||||
|
||||
return post.likes
|
||||
}
|
||||
|
||||
public async unlike ({request, params}: HttpContextContract) {
|
||||
const post = await Post.findByOrFail('slug', params.slug)
|
||||
const ip = await request.ip()
|
||||
|
||||
const getLikes = post.likes - 1
|
||||
const isLiked = await Redis.exists(`artapi/posts/${post.slug}/${ip}`)
|
||||
|
||||
if (isLiked) {
|
||||
await Redis.del(`artapi/posts/${post.slug}/${ip}`)
|
||||
await post.merge({
|
||||
likes: getLikes
|
||||
}).save()
|
||||
return {
|
||||
code: 200,
|
||||
post
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async isLiked ({params, request}: HttpContextContract) {
|
||||
const post = await Post.findBy('slug', params.slug)
|
||||
if (post) {
|
||||
const ip = request.ip()
|
||||
return Redis.exists(`artapi/posts/${post.slug}/${ip}`);
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
public async like ({request, params}: HttpContextContract) {
|
||||
let post = await Post.findBy('slug', params.slug)
|
||||
const ip = await request.ip()
|
||||
|
||||
if (!post) {
|
||||
post = await Post.create({
|
||||
slug: params.slug,
|
||||
likes: 0
|
||||
})
|
||||
}
|
||||
|
||||
const getLikes = post.likes + 1
|
||||
const isLiked = await Redis.exists(`artapi/posts/${post.slug}/${ip}`)
|
||||
|
||||
if (!isLiked) {
|
||||
await Redis.set(`artapi/posts/${post.slug}/${ip}`, Date.now())
|
||||
await post.merge({
|
||||
likes: getLikes
|
||||
}).save()
|
||||
return {
|
||||
code: 200,
|
||||
post
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
41
app/Controllers/Http/SubscribersController.ts
Normal file
41
app/Controllers/Http/SubscribersController.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import {HttpContextContract} from "@ioc:Adonis/Core/HttpContext";
|
||||
import Subscriber from "App/Models/Subscriber";
|
||||
import StoreValidator from "App/Validators/subscriber/StoreValidator";
|
||||
import UpdateValidator from "App/Validators/subscriber/UpdateValidator";
|
||||
|
||||
export default class SubscribersController {
|
||||
|
||||
public async index ({request}: HttpContextContract) {
|
||||
const limit = request.input('limit')
|
||||
const page = request.input('page')
|
||||
if (limit && page) {
|
||||
return Subscriber.query().orderBy('id', 'asc').paginate(page, limit);
|
||||
} else {
|
||||
return Subscriber.query().orderBy('id', 'asc');
|
||||
}
|
||||
}
|
||||
|
||||
public async store ({request}: HttpContextContract) {
|
||||
const data = await request.validate(StoreValidator)
|
||||
return await Subscriber.create(data)
|
||||
}
|
||||
|
||||
public async show ({params}: HttpContextContract) {
|
||||
return await Subscriber.findOrFail(params.id)
|
||||
}
|
||||
|
||||
public async update({ request, params }: HttpContextContract) {
|
||||
const subscriber = await Subscriber.findOrFail(params.id)
|
||||
const data = await request.validate(UpdateValidator)
|
||||
await subscriber.merge(data).save()
|
||||
|
||||
return { message: 'L\'abonné a été mis à jour' }
|
||||
}
|
||||
|
||||
public async destroy({ params }: HttpContextContract) {
|
||||
const subscriber = await Subscriber.findOrFail(params.id)
|
||||
await subscriber.delete()
|
||||
return { message: "L\'abonné a bien été supprimé" }
|
||||
}
|
||||
|
||||
}
|
||||
50
app/Controllers/Http/UsersController.ts
Normal file
50
app/Controllers/Http/UsersController.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import {HttpContextContract} from '@ioc:Adonis/Core/HttpContext'
|
||||
import User from "App/Models/User";
|
||||
import StoreValidator from "App/Validators/users/StoreValidator";
|
||||
import UpdateValidator from "App/Validators/users/UpdateValidator";
|
||||
|
||||
export default class UsersController {
|
||||
|
||||
public async index () {
|
||||
return User.query()
|
||||
}
|
||||
|
||||
public async store ({request}: HttpContextContract) {
|
||||
const data = await request.validate(StoreValidator)
|
||||
return await User.create(data)
|
||||
}
|
||||
|
||||
public async show ({params}: HttpContextContract) {
|
||||
return await User.findOrFail(params.id)
|
||||
}
|
||||
|
||||
public async update({ request, params, response }: HttpContextContract) {
|
||||
const user = await User.findOrFail(params.id)
|
||||
const data = await request.validate(UpdateValidator)
|
||||
const { email } = data
|
||||
const user2 = await User.findBy('email', email)
|
||||
|
||||
if (user2 !== null && user.email !== email) {
|
||||
return response.abort({
|
||||
message: 'L\' adresse mail n\'est pas unique !'
|
||||
})
|
||||
}
|
||||
|
||||
await user.merge(data).save()
|
||||
|
||||
return { message: 'Le compte a été mis à jour' }
|
||||
}
|
||||
|
||||
public async destroy({ response, params, auth }: HttpContextContract) {
|
||||
const user = await User.findOrFail(params.id)
|
||||
const admin = await User.findBy('email', 'arthurdanjou@outlook.fr')
|
||||
|
||||
if (auth.user?.id != admin?.id) {
|
||||
return response.unauthorized()
|
||||
}
|
||||
|
||||
await user.delete()
|
||||
return { message: "L'utilisateur a bien été supprimé" }
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user