public async Task <ActionMessages> Create(Person person, Guid?cityId)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(person.FirstName) || string.IsNullOrWhiteSpace(person.LastName) ||
                    string.IsNullOrWhiteSpace(person.Email) || string.IsNullOrWhiteSpace(person.PhoneNumber))
                {
                    return(ActionMessages.FillAllFields);
                }

                if (person.Age > 110 || person.Age < 1)
                {
                    return(ActionMessages.InvalidAge);
                }

                await _db.People.AddAsync(new Person { FirstName = person.FirstName, LastName = person.LastName, Age = person.Age, Email = person.Email, PhoneNumber = person.PhoneNumber, City = await _db.Cities.SingleOrDefaultAsync(x => x.Id == cityId) ?? null });

                await _db.SaveChangesAsync();

                return(ActionMessages.Created);
            }
            catch // Returns Failed if an exception were to be thrown.
            {
                return(ActionMessages.Failed);
            }
        }
Exemple #2
0
        public async Task <ActionMessages> Create(Country country)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(country.Name) || string.IsNullOrWhiteSpace(country.Population))
                {
                    return(ActionMessages.FillAllFields);
                }

                await _db.Countries.AddAsync(new Country { Name = country.Name, Population = country.Population });

                await _db.SaveChangesAsync();

                return(ActionMessages.Created);
            }
            catch (Exception ex)
            {
                throw new ArgumentException(ex.Message);
            }
        }
        public async Task <ActionMessages> Create(City city, Guid?countryId)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(city.Name) || string.IsNullOrWhiteSpace(city.Population) ||
                    string.IsNullOrWhiteSpace(city.PostalCode))
                {
                    return(ActionMessages.FillAllFields);
                }

                await _db.Cities.AddAsync(new City { Name = city.Name, PostalCode = city.PostalCode, Population = city.Population, Country = await _db.Countries.SingleOrDefaultAsync(x => x.Id == countryId) ?? null });

                await _db.SaveChangesAsync();

                return(ActionMessages.Created);
            }
            catch (Exception)
            {
                return(ActionMessages.Failed);
            }
        }