Esempio n. 1
0
        public async Task <IActionResult> EditChurch(church church)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(church);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction($"{nameof(DetailsDistrict)}/{church.IdDistrict}"));
                }
                catch (DbUpdateException dbUpdateException)
                {
                    if (dbUpdateException.InnerException.Message.Contains("duplicate"))
                    {
                        ModelState.AddModelError(string.Empty, "There are a record with the same name.");
                    }
                    else
                    {
                        ModelState.AddModelError(string.Empty, dbUpdateException.InnerException.Message);
                    }
                }
                catch (Exception exception)
                {
                    ModelState.AddModelError(string.Empty, exception.Message);
                }
            }
            return(View(church));
        }
Esempio n. 2
0
        public async Task <IActionResult> EditChurch(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }
            church church = await _context.churches.FindAsync(id);

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

            District district = await _context.districts.FirstOrDefaultAsync(d => d.Churches.FirstOrDefault(c => c.Id == church.Id) != null);

            church.IdDistrict = district.Id;
            return(View(church));
        }
Esempio n. 3
0
        public async Task <IActionResult> AddChurch(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            District district = await _context.districts.FindAsync(id);

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

            church model = new church {
                IdDistrict = district.Id
            };

            return(View(model));
        }
Esempio n. 4
0
        public async Task <IActionResult> AddChurch(church church)
        {
            if (ModelState.IsValid)
            {
                District district = await _context.districts
                                    .Include(d => d.Churches)
                                    .FirstOrDefaultAsync(c => c.Id == church.IdDistrict);

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

                try
                {
                    church.Id = 0;
                    district.Churches.Add(church);
                    _context.Update(district);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction($"{nameof(DetailsDistrict)}/{district.Id}"));
                }
                catch (DbUpdateException dbUpdateException)
                {
                    if (dbUpdateException.InnerException.Message.Contains("duplicate"))
                    {
                        ModelState.AddModelError(string.Empty, "There are a record with the same name.");
                    }
                    else
                    {
                        ModelState.AddModelError(string.Empty, dbUpdateException.InnerException.Message);
                    }
                }
                catch (Exception exception)
                {
                    ModelState.AddModelError(string.Empty, exception.Message);
                }
            }

            return(View(church));
        }
Esempio n. 5
0
        public async Task <IActionResult> DeleteChurch(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            church church = await _context.churches
                            .FirstOrDefaultAsync(m => m.Id == id);

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

            District district = await _context.districts.FirstOrDefaultAsync(d => d.Churches.FirstOrDefault(c => c.Id == church.Id) != null);

            _context.churches.Remove(church);
            await _context.SaveChangesAsync();

            return(RedirectToAction($"{nameof(DetailsDistrict)}/{district.Id}"));
        }