public HttpResponseMessage CheckIn(PatronViewModel checkIn)
        {
            ApiResponse response = new ApiResponse(Request);
            if (checkIn == null)
            {
                response.errors.Add(new NullRequest());
                return response.GenerateResponse(HttpStatusCode.BadRequest);
            }
            try
            {
                using (var database = new Database())
                {
                    var volunteerName = Request.Headers.GetHeaderValue("username");
                    var volunteer = database.Volunteers.FirstOrDefault(v => v.Username == volunteerName);
                    var serviceType = database.ServiceTypes.FirstOrDefault(st => st.ID == checkIn.ServiceSelection);
                    Patron patron;
                    if (checkIn.ID > -1)
                    {
                        patron = database.Patrons
                            .Include(p => p.Gender)
                            .Include(p => p.Addresses)
                            .Include(p => p.EmergencyContacts)
                            .Include(p => p.Ethnicity)
                            .Include(p => p.MaritalStatus)
                            .Include(p => p.PhoneNumbers)
                            .Include(p => p.ResidenceStatus)
                            .First(p => p.ID == checkIn.ID);
                        patron.Addresses = checkIn.Addresses;
                        patron.EmergencyContacts = checkIn.EmergencyContacts;
                        patron.PhoneNumbers = checkIn.PhoneNumbers;
                    }
                    else
                    {
                        patron = JsonConvert.DeserializeObject<Patron>(JsonConvert.SerializeObject(checkIn)); //You can move values up an inheritance chain!
                    }
                    patron.Ethnicity = database.Ethnicities.FirstOrDefault(et => et.ID == (int)checkIn.ethnicityID);
                    patron.Gender = database.Genders.FirstOrDefault(gt => gt.ID == (int)checkIn.genderID);
                    patron.ResidenceStatus = database.ResidenceStatuses.FirstOrDefault(rt => rt.ID == (int)checkIn.residenceStatusID);
                    patron.MaritalStatus = database.MaritalStatuses.FirstOrDefault(mt => mt.ID == (int)checkIn.maritalStatusID);

                    database.Patrons.AddOrUpdate(patron);
                    database.SaveChanges();
                    Visit visit = new Visit
                    {
                        CreateVolunteer = volunteer,
                        Service = serviceType,
                        Patron = patron
                    };
                    database.Visits.Add(visit);
                    database.SaveChanges();
                    return response.GenerateResponse(HttpStatusCode.Created);
                }
            }
            catch(Exception e)
            {
                response.errors.Add(new InvalidCheckIn { source = e });
                return response.GenerateResponse(HttpStatusCode.BadRequest);
            }
        }
Пример #2
0
        public static void Seed(Database context)
        {
            var tempUser = context.Volunteers.FirstOrDefault(v => v.Username == testUser.Username);
            var tempAdmin = context.Volunteers.FirstOrDefault(v => v.Username == testAdmin.Username);
            if (tempUser != null && tempAdmin != null) return;

            testUser.ServiceTypes = new List<ServiceType>
            {
                context.ServiceTypes.First(st => st.ID == (int) ServiceTypeID.Dental)
            };
            testAdmin.ServiceTypes = new List<ServiceType>();
            context.ServiceTypes.ToList().ForEach(st => testAdmin.ServiceTypes.Add(st));
            context.Volunteers.AddOrUpdate(
                testUser,
                testAdmin
            );

            context.SaveChanges();
        }