mirror of
https://github.com/ArthurDanjou/artdanj-api.git
synced 2026-01-27 10:00:27 +01:00
💻 | Working on API
This commit is contained in:
@@ -13,13 +13,6 @@ export default class LocationsController {
|
||||
})
|
||||
}
|
||||
|
||||
public async history ({ response }: HttpContextContract) {
|
||||
const locations = await Location.query().orderBy('since', 'desc')
|
||||
return response.status(200).send({
|
||||
locations
|
||||
})
|
||||
}
|
||||
|
||||
public async add ({ request, response }: HttpContextContract) {
|
||||
const data = await request.validate(LocationValidator)
|
||||
await Location.create(data)
|
||||
|
||||
@@ -1,74 +0,0 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import {HttpContextContract} from "@ioc:Adonis/Core/HttpContext";
|
||||
|
||||
export default class MeController {
|
||||
export default class ProfileController {
|
||||
|
||||
public me ({ response }: HttpContextContract) {
|
||||
return response.status(200).send({
|
||||
@@ -5,57 +5,54 @@ import {UpdateGitHubReadme} from "App/tasks/UpdateGithubReadme";
|
||||
export default class StatesController {
|
||||
|
||||
public async get ({response}: HttpContextContract) {
|
||||
const is_sleeping = await Redis.get('artapi/states/sleeping') || "false"
|
||||
const is_learning = await Redis.get('artapi/states/learning') || "false"
|
||||
const is_developing = await Redis.get('artapi/states/developing') || "false"
|
||||
const is_listening_music = await Redis.get('artapi/states/listening') || "false"
|
||||
const is_sleeping = await Redis.exists('artapi/states/sleeping')
|
||||
const is_listening_music = await Redis.exists('artapi/states/listening')
|
||||
const is_developing = await Redis.exists('artapi/states/developing')
|
||||
const is_learning = await Redis.exists('artapi/states/learning')
|
||||
|
||||
return response.status(200).send({
|
||||
is_sleeping: getStatus(is_sleeping),
|
||||
is_learning: getStatus(is_learning),
|
||||
is_developing: getStatus(is_developing),
|
||||
is_listening_music: getStatus(is_listening_music)
|
||||
is_learning: this.getStatus(is_learning),
|
||||
is_sleeping: this.getStatus(is_sleeping),
|
||||
is_developing: this.getStatus(is_developing),
|
||||
is_listening_music: this.getStatus(is_listening_music)
|
||||
})
|
||||
}
|
||||
|
||||
public async setSleepingStatus ({request, response}: HttpContextContract) {
|
||||
const sleeping = await request.input('sleeping')
|
||||
await Redis.set('artapi/states/sleeping', sleeping)
|
||||
await UpdateGitHubReadme()
|
||||
return response.status(200).send({
|
||||
message: 'State successfully updated !'
|
||||
})
|
||||
public async set ({request, response}: HttpContextContract) {
|
||||
const state = await request.param('state')
|
||||
const value = await request.input('value')
|
||||
|
||||
if (state && value) {
|
||||
await Redis.set(`artapi/states/${state}`, value)
|
||||
|
||||
switch (state) {
|
||||
case 'learning':
|
||||
await Redis.set(`artapi/states/developing`, 'false')
|
||||
await Redis.set(`artapi/states/sleeping`, 'false')
|
||||
break
|
||||
case 'developing':
|
||||
await Redis.set(`artapi/states/learning`, 'false')
|
||||
await Redis.set(`artapi/states/sleeping`, 'false')
|
||||
break
|
||||
case 'listening':
|
||||
await Redis.set(`artapi/states/sleeping`, 'false')
|
||||
break
|
||||
case 'sleeping':
|
||||
await Redis.set(`artapi/states/developing`, 'false')
|
||||
await Redis.set(`artapi/states/listening`, 'false')
|
||||
await Redis.set(`artapi/states/learning`, 'false')
|
||||
break
|
||||
}
|
||||
|
||||
await UpdateGitHubReadme()
|
||||
return response.status(200).send({
|
||||
message: 'State successfully updated !'
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
public async setDevelopingStatus ({request, response}: HttpContextContract) {
|
||||
const developing = await request.input('developing')
|
||||
await Redis.set('artapi/states/developing', developing)
|
||||
await UpdateGitHubReadme()
|
||||
return response.status(200).send({
|
||||
message: 'State successfully updated !'
|
||||
})
|
||||
}
|
||||
|
||||
public async setLearningStatus ({request, response}: HttpContextContract) {
|
||||
const learning = await request.input('learning')
|
||||
await Redis.set('artapi/states/learning', learning)
|
||||
await UpdateGitHubReadme()
|
||||
return response.status(200).send({
|
||||
message: 'State successfully updated !'
|
||||
})
|
||||
}
|
||||
|
||||
public async setListeningStatus ({request, response}: HttpContextContract) {
|
||||
const listening = await request.input('listening')
|
||||
await Redis.set('artapi/states/listening', listening)
|
||||
await UpdateGitHubReadme()
|
||||
return response.status(200).send({
|
||||
message: 'State successfully updated !'
|
||||
})
|
||||
public getStatus(state: number): string {
|
||||
return state === 1 ? "Yes" : "No"
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function getStatus(state: string) {
|
||||
return state === "true"
|
||||
}
|
||||
|
||||
@@ -1,54 +0,0 @@
|
||||
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
||||
import {getTotalStats, getWeeklyStats, getMonthlyStats, getDailyStats} from 'App/Helpers/StatsHelper'
|
||||
import DockerBuild from "App/Models/DockerBuild"
|
||||
import DockerCommand from "App/Models/DockerCommand"
|
||||
import {UpdateGitHubReadme} from "App/tasks/UpdateGithubReadme";
|
||||
|
||||
export default class StatsController {
|
||||
|
||||
public async get ({response}: HttpContextContract) {
|
||||
const daily = await getDailyStats()
|
||||
const weekly = await getWeeklyStats()
|
||||
const monthly = await getMonthlyStats()
|
||||
const total = await getTotalStats()
|
||||
return response.status(200).send({
|
||||
daily: daily,
|
||||
weekly: weekly,
|
||||
monthly: monthly,
|
||||
total: total,
|
||||
})
|
||||
}
|
||||
|
||||
public async incrementBuild () {
|
||||
const date = new Date()
|
||||
const last_entry = await DockerBuild.findBy('created_at', date)
|
||||
|
||||
if (last_entry) {
|
||||
last_entry.builds = last_entry.builds ++
|
||||
await last_entry.save()
|
||||
} else {
|
||||
await DockerBuild.create({
|
||||
builds: BigInt(1)
|
||||
})
|
||||
}
|
||||
|
||||
await UpdateGitHubReadme()
|
||||
}
|
||||
|
||||
public async incrementCommand () {
|
||||
const date = new Date()
|
||||
const last_entry = await DockerCommand.findBy('created_at', date)
|
||||
|
||||
if (last_entry) {
|
||||
last_entry.commands = last_entry.commands ++
|
||||
await last_entry.save()
|
||||
} else {
|
||||
await DockerCommand.create({
|
||||
commands: BigInt(1)
|
||||
})
|
||||
}
|
||||
|
||||
await UpdateGitHubReadme()
|
||||
}
|
||||
|
||||
}
|
||||
15
app/Helpers/GetWakatimeActivity.ts
Normal file
15
app/Helpers/GetWakatimeActivity.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import axios from "axios";
|
||||
import Env from "@ioc:Adonis/Core/Env";
|
||||
|
||||
export default function getActivity() {
|
||||
let last_activity = ''
|
||||
axios.get('https://wakatime.com/api/v1/users/current', {
|
||||
headers: {
|
||||
'Authorization': `Basic ${Env.get('WAKATIME_API_KEY')}`
|
||||
}
|
||||
})
|
||||
.then((res) => {
|
||||
last_activity = res.data.last_heartbeat_at
|
||||
})
|
||||
return last_activity
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
import DockerCommand from "App/Models/DockerCommand";
|
||||
import DockerBuild from "App/Models/DockerBuild";
|
||||
|
||||
async function getDailyStats() {
|
||||
const commands = await DockerCommand.query().where('created_at', '>', new Date().getTime())
|
||||
const builds = await DockerBuild.query().where('created_at', '>', new Date().getTime())
|
||||
|
||||
return {
|
||||
docker_commands_run: commands.length,
|
||||
docker_build_count: builds.length,
|
||||
}
|
||||
}
|
||||
|
||||
async function getWeeklyStats() {
|
||||
const commands = await DockerCommand.query().where('created_at', '>', new Date().getTime() - 1000 * 60 * 60 * 24 * 7)
|
||||
const builds = await DockerBuild.query().where('created_at', '>', new Date().getTime() - 1000 * 60 * 60 * 24 * 7)
|
||||
|
||||
return {
|
||||
docker_commands_run: commands.length,
|
||||
docker_build_count: builds.length,
|
||||
}
|
||||
}
|
||||
|
||||
async function getMonthlyStats() {
|
||||
const commands = await DockerCommand.query().where('created_at', '>', new Date().getMonth() - 1)
|
||||
const builds = await DockerBuild.query().where('created_at', '>', new Date().getMonth() - 1)
|
||||
|
||||
return {
|
||||
docker_commands_run: commands.length,
|
||||
docker_build_count: builds.length,
|
||||
}
|
||||
}
|
||||
|
||||
async function getTotalStats() {
|
||||
const commands = await DockerCommand.query()
|
||||
const builds = await DockerBuild.query()
|
||||
|
||||
return {
|
||||
docker_commands_run: commands.length,
|
||||
docker_build_count: builds.length
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export {getMonthlyStats, getTotalStats, getWeeklyStats, getDailyStats}
|
||||
@@ -1,13 +0,0 @@
|
||||
import { DateTime } from 'luxon'
|
||||
import { BaseModel, column } from '@ioc:Adonis/Lucid/Orm'
|
||||
|
||||
export default class DockerBuild extends BaseModel {
|
||||
@column({ isPrimary: true })
|
||||
public id: number
|
||||
|
||||
@column()
|
||||
public builds: bigint
|
||||
|
||||
@column.dateTime({ autoCreate: true })
|
||||
public createdAt: DateTime
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
import { DateTime } from 'luxon'
|
||||
import { BaseModel, column } from '@ioc:Adonis/Lucid/Orm'
|
||||
|
||||
export default class DockerCommand extends BaseModel {
|
||||
@column({ isPrimary: true })
|
||||
public id: number
|
||||
|
||||
@column()
|
||||
public commands: bigint
|
||||
|
||||
@column.dateTime({ autoCreate: true })
|
||||
public createdAt: DateTime
|
||||
}
|
||||
@@ -1,24 +1,13 @@
|
||||
import {getDailyStats, getMonthlyStats, getTotalStats, getWeeklyStats} from "App/Helpers/StatsHelper";
|
||||
import Redis from "@ioc:Adonis/Addons/Redis";
|
||||
import axios from 'axios'
|
||||
import Env from "@ioc:Adonis/Core/Env";
|
||||
|
||||
export async function UpdateGitHubReadme(): Promise<void> {
|
||||
const daily_stats = await getDailyStats()
|
||||
const weekly_stats = await getWeeklyStats()
|
||||
const monthly = await getMonthlyStats()
|
||||
const total_stats = await getTotalStats()
|
||||
|
||||
const sleeping = await Redis.get('artapi/states/sleeping')
|
||||
const learning = await Redis.get('artapi/states/learning')
|
||||
const developing = await Redis.get('artapi/states/developing')
|
||||
const listening_music = await Redis.get('artapi/states/listening')
|
||||
|
||||
const stats_table = `| Statistics | Daily | Weekly | Monthly | Total |
|
||||
| :------------------------------------------ | ----------: | ----------: | -----------: | -----------: |
|
||||
| :computer: Commands | **${daily_stats.docker_commands_run}** | **${weekly_stats.docker_commands_run}** | **${monthly.docker_commands_run}** | **${total_stats.docker_commands_run}** |
|
||||
| :hammer: Docker Builds | **${daily_stats.docker_build_count}** | **${weekly_stats.docker_build_count}** | **${monthly.docker_build_count}** | **${total_stats.docker_build_count}** |`
|
||||
|
||||
const infos_table = `| Informations | State |
|
||||
| ---------------------------: | ------: |
|
||||
| :musical_note: Music Playing | **${getStatus(listening_music)}** |
|
||||
@@ -36,27 +25,17 @@ export async function UpdateGitHubReadme(): Promise<void> {
|
||||
|
||||
const content = Buffer.from(read_me.content, 'base64').toString()
|
||||
|
||||
const stats_table_check = '| Statistics' + content.split('| Statistics')[1]
|
||||
if (!stats_table_check) change = true
|
||||
const old_stats_table = stats_table_check.split('| Informations')[0]
|
||||
if (!old_stats_table) change = true
|
||||
|
||||
const infos_table_check = '| Informations' + content.split('| Informations')[1]
|
||||
if (!infos_table_check) change = true
|
||||
const old_infos_table = infos_table_check.split('###### Curious')[0]
|
||||
if (!old_infos_table) change = true
|
||||
|
||||
if (old_infos_table == infos_table && old_stats_table == stats_table) change = false
|
||||
|
||||
if (!change) return
|
||||
|
||||
let new_content = content.replace(old_stats_table, stats_table + '\n\n');
|
||||
new_content = new_content.replace(old_infos_table, infos_table + '\n\n')
|
||||
|
||||
await axios.put('https://api.github.com/repos/ArthurDanjou/ArthurDanjou/contents/README.md',
|
||||
{
|
||||
message: 'Updating recent statistics & informations',
|
||||
content: Buffer.from(new_content, 'utf8').toString('base64'),
|
||||
content: Buffer.from(content.replace(old_infos_table, infos_table + '\n\n'), 'utf8').toString('base64'),
|
||||
sha: read_me.sha,
|
||||
author: {
|
||||
name: 'api.arthurdanjou.fr - API Automation',
|
||||
|
||||
Reference in New Issue
Block a user