-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from MagnunAVF/feat/dashboard
feat: dashboard
- Loading branch information
Showing
10 changed files
with
266 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,97 +1,125 @@ | ||
{ | ||
"info": { | ||
"_postman_id": "48895354-872d-4ddd-b78a-7bd15ce12326", | ||
"name": "demeter-api", | ||
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json", | ||
"_exporter_id": "1451659" | ||
}, | ||
"item": [ | ||
{ | ||
"name": "Create Invalid Farmer", | ||
"request": { | ||
"method": "POST", | ||
"header": [], | ||
"body": { | ||
"mode": "raw", | ||
"raw": "{\n \"name\": \"Maximus\",\n \"farm\": {\n \"name\": \"My farm\",\n \"city\": \"Cerro Largo\",\n \"state\": \"RS\",\n \"totalArea\": 100,\n \"arableArea\": 60,\n \"vegetationArea\": 40,\n \"crops\": [\"Soy\", \"Bean\"]\n }\n}", | ||
"options": { | ||
"raw": { | ||
"language": "json" | ||
} | ||
} | ||
}, | ||
"url": { | ||
"raw": "{{url}}/farmers/", | ||
"host": ["{{url}}"], | ||
"path": ["farmers", ""] | ||
} | ||
}, | ||
"response": [] | ||
}, | ||
{ | ||
"name": "Update Farmer", | ||
"request": { | ||
"method": "PUT", | ||
"header": [], | ||
"body": { | ||
"mode": "raw", | ||
"raw": "{\n \"name\": \"Maximus\",\n \"document\": \"092.875.200-38\",\n \"farm\": {\n \"name\": \"My Updated farm\",\n \"city\": \"Cerro Largo\",\n \"state\": \"RS\",\n \"totalArea\": 110,\n \"arableArea\": 60,\n \"vegetationArea\": 40,\n \"crops\": [\"Soy\"]\n }\n}", | ||
"options": { | ||
"raw": { | ||
"language": "json" | ||
} | ||
} | ||
}, | ||
"url": { | ||
"raw": "{{url}}/farmers/{{document}}", | ||
"host": ["{{url}}"], | ||
"path": ["farmers", "{{document}}"] | ||
} | ||
}, | ||
"response": [] | ||
}, | ||
{ | ||
"name": "New Request", | ||
"request": { | ||
"method": "GET", | ||
"header": [] | ||
}, | ||
"response": [] | ||
}, | ||
{ | ||
"name": "Create Farmer", | ||
"request": { | ||
"method": "POST", | ||
"header": [], | ||
"body": { | ||
"mode": "raw", | ||
"raw": "{\n \"name\": \"Maximus\",\n \"document\": \"092.875.200-38\",\n \"farm\": {\n \"name\": \"My farm\",\n \"city\": \"Cerro Largo\",\n \"state\": \"RS\",\n \"totalArea\": 100,\n \"arableArea\": 60,\n \"vegetationArea\": 40,\n \"crops\": [\"Soy\", \"Bean\"]\n }\n}", | ||
"options": { | ||
"raw": { | ||
"language": "json" | ||
} | ||
} | ||
}, | ||
"url": { | ||
"raw": "{{url}}/farmers/", | ||
"host": ["{{url}}"], | ||
"path": ["farmers", ""] | ||
} | ||
}, | ||
"response": [] | ||
}, | ||
{ | ||
"name": "Delete Farmer", | ||
"request": { | ||
"method": "DELETE", | ||
"header": [], | ||
"url": { | ||
"raw": "{{url}}/farmers/{{document}}", | ||
"host": ["{{url}}"], | ||
"path": ["farmers", "{{document}}"] | ||
} | ||
}, | ||
"response": [] | ||
} | ||
] | ||
} | ||
"info": { | ||
"_postman_id": "48895354-872d-4ddd-b78a-7bd15ce12326", | ||
"name": "demeter-api", | ||
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json", | ||
"_exporter_id": "1451659" | ||
}, | ||
"item": [ | ||
{ | ||
"name": "Create Invalid Farmer", | ||
"request": { | ||
"method": "POST", | ||
"header": [], | ||
"body": { | ||
"mode": "raw", | ||
"raw": "{\n \"name\": \"Maximus\",\n \"farm\": {\n \"name\": \"My farm\",\n \"city\": \"Cerro Largo\",\n \"state\": \"RS\",\n \"totalArea\": 100,\n \"arableArea\": 60,\n \"vegetationArea\": 40,\n \"crops\": [\"Soy\", \"Bean\"]\n }\n}", | ||
"options": { | ||
"raw": { | ||
"language": "json" | ||
} | ||
} | ||
}, | ||
"url": { | ||
"raw": "{{url}}/farmers/", | ||
"host": [ | ||
"{{url}}" | ||
], | ||
"path": [ | ||
"farmers", | ||
"" | ||
] | ||
} | ||
}, | ||
"response": [] | ||
}, | ||
{ | ||
"name": "Update Farmer", | ||
"request": { | ||
"method": "PUT", | ||
"header": [], | ||
"body": { | ||
"mode": "raw", | ||
"raw": "{\n \"name\": \"Maximus\",\n \"document\": \"092.875.200-38\",\n \"farm\": {\n \"name\": \"My Updated farm\",\n \"city\": \"Cerro Largo\",\n \"state\": \"RS\",\n \"totalArea\": 110,\n \"arableArea\": 60,\n \"vegetationArea\": 40,\n \"crops\": [\"Soy\"]\n }\n}", | ||
"options": { | ||
"raw": { | ||
"language": "json" | ||
} | ||
} | ||
}, | ||
"url": { | ||
"raw": "{{url}}/farmers/{{document}}", | ||
"host": [ | ||
"{{url}}" | ||
], | ||
"path": [ | ||
"farmers", | ||
"{{document}}" | ||
] | ||
} | ||
}, | ||
"response": [] | ||
}, | ||
{ | ||
"name": "Create Farmer", | ||
"request": { | ||
"method": "POST", | ||
"header": [], | ||
"body": { | ||
"mode": "raw", | ||
"raw": "{\n \"name\": \"Maximus\",\n \"document\": \"092.875.200-38\",\n \"farm\": {\n \"name\": \"My farm\",\n \"city\": \"Cerro Largo\",\n \"state\": \"RS\",\n \"totalArea\": 100,\n \"arableArea\": 60,\n \"vegetationArea\": 40,\n \"crops\": [\"Soy\", \"Bean\"]\n }\n}", | ||
"options": { | ||
"raw": { | ||
"language": "json" | ||
} | ||
} | ||
}, | ||
"url": { | ||
"raw": "{{url}}/farmers", | ||
"host": [ | ||
"{{url}}" | ||
], | ||
"path": [ | ||
"farmers" | ||
] | ||
} | ||
}, | ||
"response": [] | ||
}, | ||
{ | ||
"name": "Delete Farmer", | ||
"request": { | ||
"method": "DELETE", | ||
"header": [], | ||
"url": { | ||
"raw": "{{url}}/farmers/{{document}}", | ||
"host": [ | ||
"{{url}}" | ||
], | ||
"path": [ | ||
"farmers", | ||
"{{document}}" | ||
] | ||
} | ||
}, | ||
"response": [] | ||
}, | ||
{ | ||
"name": "Dashboard Data", | ||
"request": { | ||
"method": "GET", | ||
"header": [], | ||
"url": { | ||
"raw": "{{url}}/dashboard", | ||
"host": [ | ||
"{{url}}" | ||
], | ||
"path": [ | ||
"dashboard" | ||
] | ||
} | ||
}, | ||
"response": [] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
interface DashboardRepository { | ||
getFarmsTotalNumber(): Promise<number> | ||
getFarmsTotalArea(): Promise<number> | ||
} | ||
|
||
export default DashboardRepository |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import DashboardRepository from '../repository/DashboardRepository' | ||
import UseCase from './UseCase' | ||
|
||
export type DashboardArea = { | ||
totalFarms: number | ||
totalArea: number | ||
} | ||
|
||
class Dashboard implements UseCase<void, DashboardArea> { | ||
constructor(private dashboardRepository: DashboardRepository) {} | ||
|
||
async execute(): Promise<DashboardArea> { | ||
const totalFarms = await this.dashboardRepository.getFarmsTotalNumber() | ||
const totalArea = await this.dashboardRepository.getFarmsTotalArea() | ||
|
||
return { | ||
totalArea, | ||
totalFarms, | ||
} | ||
} | ||
} | ||
|
||
export default Dashboard |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { Express } from 'express' | ||
import Dashboard from '../../core/useCases/Dashboard' | ||
import DashboardDto from './dtos/DashboardDto' | ||
|
||
class DashboardController { | ||
constructor(server: Express, useCase: Dashboard) { | ||
server.get('/dashboard', async (req, resp) => { | ||
try { | ||
const dashboardData: DashboardDto = await useCase.execute() | ||
|
||
resp.status(200).send(dashboardData) | ||
} catch (error: any) { | ||
console.log(`Error in Dashboard: ${error}`) | ||
|
||
resp.status(500).send('Internal Server Error') | ||
} | ||
}) | ||
} | ||
} | ||
|
||
export default DashboardController |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
interface DashboardDto { | ||
totalFarms: number | ||
totalArea: number | ||
} | ||
|
||
export default DashboardDto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { PrismaClient } from '@prisma/client' | ||
import DashboardRepository from '../../core/repository/DashboardRepository' | ||
|
||
class PgDashboardRepository implements DashboardRepository { | ||
prisma = new PrismaClient() | ||
|
||
async getFarmsTotalNumber(): Promise<number> { | ||
const totalFarms = await this.prisma.farm.count() | ||
|
||
return totalFarms | ||
} | ||
|
||
async getFarmsTotalArea(): Promise<number> { | ||
const sumTotalArea = await this.prisma.farm.aggregate({ | ||
_sum: { | ||
totalArea: true, | ||
}, | ||
}) | ||
const data = sumTotalArea['_sum']['totalArea'] | ||
|
||
return data | ||
} | ||
} | ||
|
||
export default PgDashboardRepository |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.