public async Task <IActionResult> Delete(int id)
        {
            DepartmentContact departmentContact = _repository.GetDepartmentContactsOnContactId(id);

            _repository.Delete(departmentContact);
            await _repository.SaveChangesAsync();

            return(RedirectToAction(nameof(HomeController.Index)));
        }
        public async Task <IActionResult> Put([FromBody] Message[] jsonPeople)
        {
            if (jsonPeople[0].DepartmentName == null || jsonPeople[0].DepartmentName == "")
            {
                List <DepartmentContact> departmentContacts = await _repository.GetAsync().ConfigureAwait(false);

                foreach (Message p in jsonPeople)
                {
                    string matchingDepartmentName = null;
                    try
                    {
                        HttpRequestMessage  request  = new HttpRequestMessage(HttpMethod.Get, $"Home/GetDepartmentName/{p.DepartmentId}");
                        HttpClient          client   = _clientFactory.CreateClient("departments");
                        HttpResponseMessage response = await client.SendAsync(request);

                        matchingDepartmentName = await response.Content.ReadAsStringAsync();
                    }
                    catch (Exception)
                    {
                        // Log
                    }

                    DepartmentContact departmentContact = departmentContacts.Where(c => c.ContactId == p.Id).First();
                    departmentContact.ContactId      = p.Id;
                    departmentContact.ContactName    = p.Name;
                    departmentContact.DepartmentId   = Int32.Parse(p.DepartmentId);
                    departmentContact.DepartmentName = matchingDepartmentName ?? "Unknown Department";
                    departmentContact.LastUpdated    = DateTime.Now;
                }

                _repository.UpdateRange(departmentContacts);
                await _repository.SaveChangesAsync();

                return(Ok());
            }
            else
            {
                IEnumerable <DepartmentContact> contactsToUpdate = _repository.GetDepartmentContactsOnDepartmentId(Int32.Parse(jsonPeople[0].DepartmentId));
                contactsToUpdate.ToList().ForEach(c => c.DepartmentName = jsonPeople[0].DepartmentName);

                if (ModelState.IsValid)
                {
                    _repository.UpdateRange(contactsToUpdate);
                    await _repository.SaveChangesAsync();
                }

                return(Ok());
            }
        }
コード例 #3
0
        // GET: Home/Details/{id:int?}
        public async Task <IActionResult> Details(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            DepartmentContact departmentContact = await _repository.GetByIDAsync(id.Value).ConfigureAwait(false);

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

            return(View(departmentContact));
        }
        public async Task <IActionResult> Post([FromBody] Message jsonPerson)
        {
            List <DepartmentContact> departmentContacts = await _repository.GetAsync().ConfigureAwait(false);

            string matchingDepartmentName = null;

            try
            {
                HttpRequestMessage  request  = new HttpRequestMessage(HttpMethod.Get, $"Home/GetDepartmentName/{jsonPerson.DepartmentId}");
                HttpClient          client   = _clientFactory.CreateClient("departments");
                HttpResponseMessage response = await client.SendAsync(request);

                matchingDepartmentName = await response.Content.ReadAsStringAsync();
            }
            catch (Exception)
            {
                // Log
            }

            DepartmentContact departmentContact = new DepartmentContact
            {
                Id             = departmentContacts.Count,
                ContactId      = jsonPerson.Id,
                ContactName    = jsonPerson.Name,
                DepartmentId   = Int32.Parse(jsonPerson.DepartmentId),
                DepartmentName = matchingDepartmentName ?? "Unknown Department",
                LastUpdated    = DateTime.Now
            };

            if (ModelState.IsValid)
            {
                _repository.Insert(departmentContact);
                await _repository.SaveChangesAsync();
            }

            return(Ok());
        }