Esempio n. 1
0
        /// <summary>
        /// This method shows the information of the selected pupil
        /// <example>Pupils/Details/1</example>
        /// <example>Pupils/Details/4</example>
        /// </summary>
        /// <param name="id">ID of the selected pupil</param>
        /// <returns>Details of the Pupil whose ID is given</returns>

        public ActionResult Details(int id)
        {
            ShowPupil showPupil = new ShowPupil();

            //Get the current pupil from the database
            string url = "Pupildata/FindPupil/" + id;
            HttpResponseMessage response = client.GetAsync(url).Result;

            if (response.IsSuccessStatusCode)
            {
                PupilDto SelectedPupil = response.Content.ReadAsAsync <PupilDto>().Result;
                showPupil.pupil = SelectedPupil;

                //Get the Classe to which the pupil belongs
                url      = "PupilData/GetPupilClasse/" + id;
                response = client.GetAsync(url).Result;
                ClasseDto SelectedClasse = response.Content.ReadAsAsync <ClasseDto>().Result;
                showPupil.classe = SelectedClasse;

                //Get the location of the selected pupil
                url      = "PupilData/GetPupilLocation/" + id;
                response = client.GetAsync(url).Result;
                LocationDto SelectedLocation = response.Content.ReadAsAsync <LocationDto>().Result;
                showPupil.location = SelectedLocation;

                return(View(showPupil));
            }
            else
            {
                return(RedirectToAction("Error"));
            }
        }
        public IHttpActionResult GetPupils()
        {
            //List of the pupils from the database
            List <Pupil> Pupils = db.Pupils.ToList();

            //Data transfer object to show information about the pupil
            List <ShowPupil> PupilDtos = new List <ShowPupil> {
            };

            foreach (var Pupil in Pupils)
            {
                ShowPupil pupil = new ShowPupil();

                //Get the Classe to which the Pupil belongs to
                Classe classe = db.Classes.Where(c => c.Pupils.Any(m => m.pId == Pupil.pId)).FirstOrDefault();

                ClasseDto parentClass = new ClasseDto
                {
                    classId   = classe.classId,
                    className = classe.className,
                    startDate = classe.startDate,
                    endDate   = classe.endDate
                };
                //Get the location of pupil
                Location location = db.Locations.Where(l => l.Pupils.Any(m => m.pId == Pupil.pId)).FirstOrDefault();

                LocationDto locationPlace = new LocationDto
                {
                    locId       = location.locId,
                    city        = location.city,
                    country     = location.country,
                    incomeRange = location.incomeRange
                };



                PupilDto NewPupil = new PupilDto
                {
                    pId       = Pupil.pId,
                    firstName = Pupil.firstName,
                    lastName  = Pupil.lastName,
                    age       = Pupil.age,
                    classId   = Pupil.classId,
                    locId     = Pupil.locId
                };

                pupil.pupil    = NewPupil;
                pupil.classe   = parentClass;
                pupil.location = locationPlace;
                PupilDtos.Add(pupil);
            }

            return(Ok(PupilDtos));
        }