Browse Source

docs: Add comments to logs service and controller

master
Konstantinos Kamaropoulos 5 years ago
parent
commit
dafc2718dd
  1. 15
      src/logs/logs.controller.ts
  2. 7
      src/logs/logs.service.ts

15
src/logs/logs.controller.ts

@ -6,21 +6,30 @@ import { ILogs } from './logs.interface';
export class LogsController {
constructor(private readonly logsService: LogsService) {}
// GET /logs Endpoint
// Get all logs from Mongo
@Get()
async getPosts() {
async getLogs() {
// Get and return logs from logsRervice
return this.logsService.getLogs();
}
// POST /logs Endpoint
// Store a new log into Mongo
@Post()
async postLogs(@Body() body, @Response() res: any) {
let success: Boolean = await this.logsService.storeLog(body);
// Try to store data into Mongo
let success: Boolean = await this.logsService.storeLog(body);
// If the operation was successful
if (success){
// respond with HTTP Code 201 CREATED
// and a success message
return res.status(HttpStatus.CREATED)
.json({
message: "Log stored successfully!"
});
} else {
// If not, respond with 500, internal server error and an error message
return res.status(HttpStatus.INTERNAL_SERVER_ERROR)
.json({
message: "An error occured during storing the log."

7
src/logs/logs.service.ts

@ -6,22 +6,29 @@ import { Model } from 'mongoose';
@Injectable()
export class LogsService {
constructor(@InjectModel('Logs') private readonly logsModel: Model<ILogs>) {}
// Method to get all logs from Mongo
async getLogs(): Promise<Object> {
return await this.logsModel.find();
}
// Method to get the count of the logs
getLogsCount(): Number {
return this.logsModel.count();
}
// Store logs into Mongo collection
async storeLog(data: Object): Promise<Boolean>{
// Create new Model object with data from request
const storedLog = new this.logsModel({
drone_id: data["id"],
gprmc: data["GPRMC"],
gps_data: data["gps_data"],
sensorReadings: data["sensorReadings"]
});
// Store data to Mongo
let id = await storedLog.save();
// Print out the Mongo DB response
console.log(id);
return true;
}

Loading…
Cancel
Save