Skip to content

Commit

Permalink
feat: added id to model
Browse files Browse the repository at this point in the history
  • Loading branch information
araujo88 committed Apr 9, 2024
1 parent d0e8e07 commit 2ebc82e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 7 deletions.
11 changes: 8 additions & 3 deletions include/models/person.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@
#include <string>

class Person {
public:
private:
static unsigned int num_instances;
unsigned int id;
std::string name;
unsigned int age;

Person() = default;
Person(std::string name, unsigned int age) : name(name), age(age) {}
public:
Person(std::string name, unsigned int age);
unsigned int getId() const;
std::string getName() const;
unsigned int getAge() const;
};
5 changes: 3 additions & 2 deletions include/services/person_service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ class IPersonService {
public:
virtual void addPerson(const Person &person) = 0;
virtual std::vector<Person> getAllPersons() = 0;
// virtual Person getPerson(unsigned int id) = 0;
};

class PersonService : public IPersonService {
private:
std::vector<Person> persons;

public:
void addPerson(const Person &person);
std::vector<Person> getAllPersons();
void addPerson(const Person &person) override;
std::vector<Person> getAllPersons() override;
};
16 changes: 16 additions & 0 deletions src/models/person.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "../../include/models/person.hpp"

unsigned int Person::num_instances = 0;

Person::Person(std::string name, unsigned int age) {
this->name = name;
this->age = age;
this->id = num_instances;
this->num_instances++;
}

unsigned int Person::getId() const { return this->id; }

std::string Person::getName() const { return this->name; }

unsigned int Person::getAge() const { return this->age; }
5 changes: 3 additions & 2 deletions src/serializers/person_serializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

boost::json::object PersonSerializer::toJson(const Person &person) {
boost::json::object obj;
obj["name"] = person.name;
obj["age"] = person.age;
obj["id"] = person.getId();
obj["name"] = person.getName();
obj["age"] = person.getAge();
return obj;
}

Expand Down

0 comments on commit 2ebc82e

Please sign in to comment.