Compare commits

...

2 Commits

Author SHA1 Message Date
Konstantinos Kamaropoulos dafc2718dd docs: Add comments to logs service and controller 5 years ago
Konstantinos Kamaropoulos 4dbe016477 feat: Remove GET / endpoint since it's not being used 5 years ago
  1. 5
      src/app.controller.ts
  2. 15
      src/logs/logs.controller.ts
  3. 18
      src/logs/logs.service.spec.ts
  4. 7
      src/logs/logs.service.ts

5
src/app.controller.ts

@ -4,9 +4,4 @@ import { AppService } from './app.service';
@Controller() @Controller()
export class AppController { export class AppController {
constructor(private readonly appService: AppService) {} constructor(private readonly appService: AppService) {}
@Get()
getHello(): string {
return this.appService.getHello();
}
} }

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."

18
src/logs/logs.service.spec.ts

@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { LogsService } from './logs.service';
describe('LogsService', () => {
let service: LogsService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [LogsService],
}).compile();
service = module.get<LogsService>(LogsService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});

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