Esempio n. 1
0
        public async Task <IActionResult> Post([FromBody] StateOrProvinceForm model)
        {
            if (ModelState.IsValid)
            {
                var country = await _countryRepository.Query().FirstOrDefaultAsync(x => x.Id == model.CountryId);

                if (country == null)
                {
                    return(NotFound());
                }

                var stateProvince = new StateOrProvince
                {
                    Name      = model.Name,
                    Code      = model.Code,
                    CountryId = country.Id,
                    Country   = country,
                    Type      = model.Type
                };
                _stateOrProvinceRepository.Add(stateProvince);
                await _stateOrProvinceRepository.SaveChangesAsync();

                return(CreatedAtAction(nameof(Get), new { id = stateProvince.Id }, null));
            }
            return(BadRequest(ModelState));
        }
Esempio n. 2
0
        public async Task <IActionResult> Get(long id)
        {
            var stateProvince = await _stateOrProvinceRepository.Query().FirstOrDefaultAsync(x => x.Id == id);

            if (stateProvince == null)
            {
                return(NotFound());
            }

            var model = new StateOrProvinceForm
            {
                Id        = stateProvince.Id,
                Name      = stateProvince.Name,
                Code      = stateProvince.Code,
                CountryId = stateProvince.CountryId,
                Type      = stateProvince.Type
            };

            return(Json(model));
        }
Esempio n. 3
0
        public async Task <IActionResult> Put(long id, [FromBody] StateOrProvinceForm model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var stateProvince = await _stateOrProvinceRepository.Query().FirstOrDefaultAsync(x => x.Id == id);

            if (stateProvince == null)
            {
                return(NotFound());
            }

            stateProvince.Name = model.Name;
            stateProvince.Code = model.Code;
            stateProvince.Type = model.Type;

            await _stateOrProvinceRepository.SaveChangesAsync();

            return(Accepted());
        }