Exemplo n.º 1
0
 public async Task <IEnumerable <IAgeGroupData> > GetAgeGroups()
 {
     using (var context = new AgeRangerContext())
     {
         return(await Task.Run(() => context.AgeGroup.ToList <IAgeGroupData>()));
     }
 }
Exemplo n.º 2
0
 public PersonController(AgeRangerContext ctx, HttpStatusMapper statusProvider, IOptions <AppModuleConfig> appConfig)
 {
     _ctx            = ctx;
     _statusProvider = statusProvider;
     _provider       = new AgeRangerDataProvider(ctx);
     _appConfig      = appConfig;
 }
        public async Task <IPersonData> AddOrUpdate(long id, string firstName, string lastName, long?age)
        {
            using (var context = new AgeRangerContext())
            {
                var dbPerson = await Task.Run(() => context.Person.FirstOrDefault(p => p.Id == id));

                if (dbPerson == null)
                {
                    dbPerson = new Person()
                    {
                        FirstName = firstName,
                        LastName  = lastName,
                        Age       = age
                    };

                    context.Add(dbPerson);
                }
                else
                {
                    dbPerson.Age       = age;
                    dbPerson.FirstName = firstName;
                    dbPerson.LastName  = lastName;
                }

                await Task.Run(() => context.SaveChanges());

                return(dbPerson);
            }
        }
 public async Task <IPersonData> GetPerson(long id)
 {
     using (var context = new AgeRangerContext())
     {
         return(await Task.Run(() => context.Person.FirstOrDefault(p => p.Id == id)));
     }
 }
 public async Task <IEnumerable <IPersonData> > GetPersons()
 {
     using (var context = new AgeRangerContext())
     {
         return(await Task.Run(() => context.Person.ToList <IPersonData>()));
     }
 }
Exemplo n.º 6
0
 public JsonResult GetItem(int id)
 {
     using (var dataContext = new AgeRangerContext())
     {
         var item = dataContext.People.FirstOrDefault(p => p.Id == id);
         return(Json(item, JsonRequestBehavior.AllowGet));
     }
 }
        public async Task Delete(long id)
        {
            using (var context = new AgeRangerContext())
            {
                var dbPerson = await Task.Run(() => context.Person.FirstOrDefault(p => p.Id == id));

                if (dbPerson != null)
                {
                    context.Person.Remove(dbPerson);
                }

                await Task.Run(() => context.SaveChanges());
            }
        }
Exemplo n.º 8
0
 public string AddItem(Person p)
 {
     if (p != null)
     {
         using (var dataContext = new AgeRangerContext())
         {
             dataContext.People.Add(p);
             dataContext.SaveChanges();
             return("Person added.");
         }
     }
     else
     {
         return("Invalid Person.");
     }
 }
Exemplo n.º 9
0
        public IEnumerable <PersonAgeGroup> Execute(string searchTerm)
        {
            using (var dataContext = new AgeRangerContext())
            {
                var people    = dataContext.People.AsQueryable();
                var ageGroups = dataContext.AgeGroups.AsQueryable();

                var result = from p in people
                             from a in ageGroups
                             where
                             (p.Age >= a.MinAge || a.MinAge == null || (a.MinAge * 1) == 0) &&
                             (p.Age < a.MaxAge || a.MaxAge == null || (a.MaxAge * 1) == 0)
                             select
                             new
                {
                    Id          = p.Id,
                    FirstName   = p.FirstName,
                    LastName    = p.LastName,
                    Age         = p.Age,
                    Description = a.Description
                };

                if (!String.IsNullOrWhiteSpace(searchTerm))
                {
                    result =
                        result.Where(
                            p =>
                            p.FirstName.ToLower().Contains(searchTerm.ToLower()) ||
                            p.LastName.ToLower().Contains(searchTerm.ToLower()));
                }

                return
                    (result.Select(
                         r =>
                         new PersonAgeGroup
                {
                    Id = r.Id,
                    FirstName = r.FirstName,
                    LastName = r.LastName,
                    Age = r.Age,
                    Description = r.Description
                }).ToArray());
            }
        }
Exemplo n.º 10
0
 public string UpdateItem(Person person)
 {
     if (person != null)
     {
         using (var dataContext = new AgeRangerContext())
         {
             var p = dataContext.People.FirstOrDefault(e => e.Id == person.Id);
             if (p == null)
             {
                 return("Invalid person.");
             }
             p.FirstName = person.FirstName;
             p.LastName  = person.LastName;
             p.Age       = person.Age;
             dataContext.SaveChanges();
             return("Person updated.");
         }
     }
     else
     {
         return("Invalid person.");
     }
 }