From f272d52f10cf3381fe60b4700b215350a0482643 Mon Sep 17 00:00:00 2001
From: Guillem <guillem@lordwektabyte.cat>
Date: Sat, 2 Apr 2022 16:13:51 +0200
Subject: [PATCH] Complet exercici 3d

---
 UOC20212/UOCVaccine/src/api.c | 50 ++++++++++++++++++++++++++++++++---
 1 file changed, 47 insertions(+), 3 deletions(-)

diff --git a/UOC20212/UOCVaccine/src/api.c b/UOC20212/UOCVaccine/src/api.c
index 5c9c420..94ce6bf 100644
--- a/UOC20212/UOCVaccine/src/api.c
+++ b/UOC20212/UOCVaccine/src/api.c
@@ -20,6 +20,8 @@
 #define MAX_PERSON_EMAIL_LEN 30
 #define MAX_PERSON_ADDRESS_LEN 30
 #define VACCINE_TYPE_NAME "VACCINE"
+#define DATE_LEN 10
+#define TIME_LEN 5
 
 // Get the API version information
 const char* api_version() {
@@ -280,13 +282,15 @@ tApiError api_getVaccineLot(tApiData data, const char* cp, const char* vaccine,
     
         vaccineLot_cpy(&found,data.vaccineLots.elems[index]);
       
-        // Fill buffer with data from the vaccine on that pointer
+        // Fill buffer with data from the vaccineLot
         sprintf(buffer, "%02d/%02d/%04d;%02d:%02d;%s;%s;%d;%d;%d", 
             found.timestamp.date.day,found.timestamp.date.month,found.timestamp.date.year,
             found.timestamp.time.hour,found.timestamp.time.minutes,
             found.cp,
             found.vaccine->name,found.vaccine->required,found.vaccine->days,
             found.doses);
+            
+        vaccineLot_free(&found);
         
         // Init CSV entry and set values accordingly
         csv_initEntry(entry);
@@ -325,7 +329,7 @@ tApiError api_getVaccines(tApiData data, tCSVData *vaccines) {
         // Reuse previous function to get a vaccine by name and have it stored in a CSVEntry
         if (api_getVaccine(data,currentVaccine->vaccine.name,&foundVaccine) == E_SUCCESS) {
 
-            // Read vaccine info from getVaccine() method
+            // Read vaccine info
             csv_getAsString(foundVaccine,0,name,MAX_VACCINE_NAME_LEN+1);
             required = csv_getAsInteger(foundVaccine,1);
             days = csv_getAsInteger(foundVaccine,2);
@@ -352,5 +356,45 @@ tApiError api_getVaccineLots(tApiData data, tCSVData *lots) {
     //////////////////////////////////
     // Ex PR1 3d
     /////////////////////////////////
-    return E_NOT_IMPLEMENTED; 
+    
+    tApiError ret = E_NOT_IMPLEMENTED;
+    tCSVEntry foundLot;
+    tVaccineLot currentVaccineLot;
+    char buffer[FILE_READ_BUFFER_SIZE];
+    tDateTime timestamp;
+    char date[DATE_LEN];
+    char time[TIME_LEN];
+    char cp [MAX_CP_LEN];
+    char vaccine [MAX_VACCINE_NAME_LEN];
+    int required;
+    int days;
+    int doses;
+    
+    csv_init(lots);
+    
+    for (int i = 0; i < data.vaccineLots.count; i++) {
+        
+        vaccineLot_cpy(&currentVaccineLot,data.vaccineLots.elems[i]);
+        csv_initEntry(&foundLot);
+        
+        if (api_getVaccineLot(data, currentVaccineLot.cp, currentVaccineLot.vaccine->name, currentVaccineLot.timestamp, &foundLot) == E_SUCCESS) {
+            
+            // Fill buffer with data from the vaccinelot
+            sprintf(buffer, "%02d/%02d/%04d;%02d:%02d;%s;%s;%d;%d;%d", 
+                currentVaccineLot.timestamp.date.day,currentVaccineLot.timestamp.date.month,currentVaccineLot.timestamp.date.year,
+                currentVaccineLot.timestamp.time.hour,currentVaccineLot.timestamp.time.minutes,
+                currentVaccineLot.cp,
+                currentVaccineLot.vaccine->name,currentVaccineLot.vaccine->required,currentVaccineLot.vaccine->days,
+                currentVaccineLot.doses);
+                
+            csv_addStrEntry(lots,buffer,VACCINE_LOT_TYPE_NAME);
+            
+            vaccineLot_free(&currentVaccineLot);
+            
+            ret = E_SUCCESS;
+            
+        }
+        csv_freeEntry(&foundLot);
+    }
+    return ret; 
 }