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 { export class LogsController {
constructor(private readonly logsService: LogsService) {} constructor(private readonly logsService: LogsService) {}
// GET /logs Endpoint
// Get all logs from Mongo
@Get() @Get()
async getPosts() { async getLogs() {
// Get and return logs from logsRervice
return this.logsService.getLogs(); return this.logsService.getLogs();
} }
// POST /logs Endpoint
// Store a new log into Mongo
@Post() @Post()
async postLogs(@Body() body, @Response() res: any) { 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){ if (success){
// respond with HTTP Code 201 CREATED
// and a success message
return res.status(HttpStatus.CREATED) return res.status(HttpStatus.CREATED)
.json({ .json({
message: "Log stored successfully!" message: "Log stored successfully!"
}); });
} else { } else {
// If not, respond with 500, internal server error and an error message
return res.status(HttpStatus.INTERNAL_SERVER_ERROR) return res.status(HttpStatus.INTERNAL_SERVER_ERROR)
.json({ .json({
message: "An error occured during storing the log." message: "An error occured during storing the log."

7
src/logs/logs.service.ts

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

Loading…
Cancel
Save