From 36bac13b1bdc11696ffd5575bce5847463e8195b Mon Sep 17 00:00:00 2001 From: Guillem Date: Sat, 2 Apr 2022 12:23:16 +0200 Subject: [PATCH] Complet exercici 2f --- UOC20212/UOCVaccine/src/api.c | 51 ++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 4 deletions(-) diff --git a/UOC20212/UOCVaccine/src/api.c b/UOC20212/UOCVaccine/src/api.c index 3828211..5419f04 100644 --- a/UOC20212/UOCVaccine/src/api.c +++ b/UOC20212/UOCVaccine/src/api.c @@ -13,6 +13,12 @@ #define VACCINE_LOT_NUM_FIELDS 7 #define MAX_CP_LEN 5 #define MAX_VACCINE_NAME_LEN 15 +#define PERSON_TYPE_NAME "PERSON" +#define PERSON_NUM_FIELDS 7 +#define MAX_PERSON_DOCUMENT_LEN 9 +#define MAX_PERSON_NAME_LEN 15 +#define MAX_PERSON_EMAIL_LEN 30 +#define MAX_PERSON_ADDRESS_LEN 30 // Get the API version information const char* api_version() { @@ -168,9 +174,9 @@ tApiError api_freeData(tApiData* data) { // Ex PR1 2e ///////////////////////////////// - population_free(&data->population); - vaccineList_free(&data->vaccines); - vaccineLotData_free(&data->vaccineLots); + population_free(&(data->population)); + vaccineList_free(&(data->vaccines)); + vaccineLotData_free(&(data->vaccineLots)); return E_SUCCESS; } @@ -184,7 +190,44 @@ tApiError api_addDataEntry(tApiData* data, tCSVEntry entry) { tApiError ret = E_NOT_IMPLEMENTED; - + // Check if entry type is Vaccine Lot and its field number is correct + if (strcmp(csv_getType(&entry),VACCINE_LOT_TYPE_NAME) == 0) { + if (csv_numFields(entry) == VACCINE_LOT_NUM_FIELDS) { + + ret = api_addVaccineLot(data,entry); + + } else { // Num fields does not match + ret = E_INVALID_ENTRY_FORMAT; + } + + // Check if entry type is Person and its field number is correct + } else if (strcmp(csv_getType(&entry),PERSON_TYPE_NAME) == 0) { + if (csv_numFields(entry) == PERSON_NUM_FIELDS) { + + // Process could be implemented in a "api_addPerson()" function + // as in "api_addVaccineLot()" + + tPerson entryPerson; + person_init(&entryPerson); + person_parse(&entryPerson,entry); + + // Check if person already exists + if (population_find(data->population,entryPerson.document) == -1) { + population_add(&(data->population),entryPerson); + ret = E_SUCCESS; + } else { + ret = E_DUPLICATED_PERSON; + } + + person_free(&entryPerson); + + } else { // Num fields does not match + ret = E_INVALID_ENTRY_FORMAT; + } + + } else { + ret = E_INVALID_ENTRY_TYPE; + } return ret; }