Beispiel #1
0
        public IActionResult Get()
        {
            logger.LogInformation($"Method Get api/home");
            List <Student> students       = new List <Student>();
            List <Student> cachedStudents = new List <Student>();

            if (dataType == "json")
            {
                students = JsonParser.Read(fileNameJson);
            }
            if (dataType == "xml")
            {
                students = XMLParser.Read(fileNameXML);
            }
            if (students == null)
            {
                return(NotFound());
            }
            List <Student> newStudents = new List <Student>();

            if (cache.Count != 0)
            {
                cachedStudents = cache.GetAllValues();
                for (int i = 0; i < cache.Count; i++)
                {
                    newStudents.Add(cachedStudents[i]);
                }

                foreach (Student s in newStudents)
                {
                    logger.LogInformation($"Students in the initial cache: {s.FirstName} {s.LastName}");
                }

                students.RemoveAll(x => newStudents.Contains(x));

                for (int i = 0; i < students.Count; i++)
                {
                    newStudents.Add(students[i]);
                    cache.Add(students[i].Id, students[i]);
                    string name = students[i].FirstName;
                    logger.LogInformation($"Not in the cache: {name} {students[i].LastName}");
                    logger.LogInformation($"Added to the cache: {name} {students[i].LastName}");
                }
            }
            else
            {
                for (int i = 0; i < students.Count; i++)
                {
                    cache.Add(students[i].Id, students[i]);
                    newStudents.Add(students[i]);
                    string name = newStudents[i].FirstName;
                    logger.LogInformation($"Added to new cache: {name} {students[i].LastName}");
                }
            }
            logger.LogInformation($"Cache Count: {cache.Count}");
            logger.LogInformation($"Cache Capacity: {cache.Capacity}");
            foreach (Student s in cache.GetAllValues())
            {
                logger.LogInformation($"Students in new cache: {s.FirstName} {s.LastName}");
            }
            return(Ok(newStudents));
        }