private void UpdateResident(RegisterResidentViewModel model) { try { ResidentsInformation resident = entities.ResidentsInformations.Where(m => m.ResidentId == model.ResidentID).FirstOrDefault(); resident.FirstName = model.FirstName; resident.LastName = model.LastName; resident.MiddleName = model.MiddleName; resident.Birthday = model.Birthdate; resident.Sex = model.Sex; resident.DateRecorded = model.DateRecorded; entities.Entry(resident).State = System.Data.Entity.EntityState.Modified; entities.SaveChanges(); if (resident.ResidentsLocations.FirstOrDefault() == null) { HouseHoldAddress houseHoldAddress = new HouseHoldAddress() { AddressId = KeyGenerator.GenerateId(model.Address), Address = model.Address, SiteId = model.SiteID }; entities.HouseHoldAddresses.Add(houseHoldAddress); entities.SaveChanges(); ResidentsLocation residentsLocation = new ResidentsLocation() { ResidentLocationId = KeyGenerator.GenerateId(model.ResidentID + model.Address), ResidentId = model.ResidentID, AddressId = houseHoldAddress.AddressId }; entities.ResidentsLocations.Add(residentsLocation); entities.SaveChanges(); } else { HouseHoldAddress houseHoldAddress = resident.ResidentsLocations.FirstOrDefault().HouseHoldAddress; houseHoldAddress.Address = model.Address; houseHoldAddress.SiteId = model.SiteID; entities.Entry(resident).State = System.Data.Entity.EntityState.Modified; entities.SaveChanges(); } TempData["alert-type"] = "alert-success"; TempData["alert-header"] = "Success"; TempData["alert-msg"] = model.FirstName + " " + model.LastName + " record was successfully updated."; // Audit Trail string userId = User.Identity.GetUserId(); new AuditTrailer().Record(model.FirstName + " " + model.LastName + "'s records were updated and modified.", AuditTrailer.RESIDENT_TYPE, userId); } catch (Exception e) { TempData["alert-type"] = "alert-danger"; TempData["alert-header"] = "Error"; TempData["alert-msg"] = "Something went wrong, please try again later."; } }
private void AddResident(RegisterResidentViewModel model) { try { ResidentsInformation resident = new ResidentsInformation() { ResidentId = KeyGenerator.GenerateId(model.FirstName + model.LastName), FirstName = model.FirstName, MiddleName = model.MiddleName, LastName = model.LastName, Sex = model.Sex, Birthday = model.Birthdate, DateRecorded = DateTime.Now }; entities.ResidentsInformations.Add(resident); entities.SaveChanges(); HouseHoldAddress houseHoldAddress = new HouseHoldAddress() { AddressId = KeyGenerator.GenerateId(model.Address), Address = model.Address, SiteId = model.SiteID }; entities.HouseHoldAddresses.Add(houseHoldAddress); entities.SaveChanges(); ResidentsLocation residentsLocation = new ResidentsLocation() { ResidentLocationId = KeyGenerator.GenerateId(model.ResidentID + model.Address), ResidentId = resident.ResidentId, AddressId = houseHoldAddress.AddressId }; entities.ResidentsLocations.Add(residentsLocation); entities.SaveChanges(); TempData["alert-type"] = "alert-success"; TempData["alert-header"] = "Success"; TempData["alert-msg"] = model.FirstName + " " + model.LastName + " was successfully recorded as a resident of Sinisian."; // Audit Trail string userId = User.Identity.GetUserId(); new AuditTrailer().Record(model.FirstName + " " + model.LastName + " is created a record as Sinisan resident.", AuditTrailer.RESIDENT_TYPE, userId); } catch (Exception e) { TempData["alert-type"] = "alert-danger"; TempData["alert-header"] = "Error"; TempData["alert-msg"] = "Something went wrong, please try again later."; } }