public IActionResult AddTeacher(TeacherModel teacher) { try { if (!ModelState.IsValid) { return(BadRequest("İstek geçerli değil.")); } using (UnitOfWork uow = new UnitOfWork()) { teacher.TeacherPassword = CryptoExtensions.Md5Encrypt(teacher.TeacherPassword); uow.GetRepository <TeacherModel>().Add(teacher); return(uow.SaveChanges() > 0 ? Ok(teacher) : StatusCode(424, "Beklenmeyen hata gerçekleşti.")); } } catch (Exception e) { return(StatusCode(500, e.Message)); } }
private List <HistoryCollectionModel> ChangeDataCapture() { List <HistoryCollectionModel> historyCollection = new List <HistoryCollectionModel>(); var changeTrack = DbContext.ChangeTracker.Entries() .Where(p => p.State == EntityState.Deleted || p.State == EntityState.Modified); foreach (var entry in changeTrack) { if (entry.Entity != null) { HistoryCollectionModel historyModel = new HistoryCollectionModel(); historyModel.DatabaseName = DbContext.Database.GetDbConnection().Database; historyModel.ObjectName = DbContext.Entry(entry.Entity).Metadata.Name; historyModel.ChangeType = entry.State.ToString(); historyModel.DateTime = DateTime.Now; historyModel.Columns = new List <HistoryTableColumnsModel>(); foreach (var prop in entry.OriginalValues.Properties) { if (string.Equals(prop.Name, _unitOfWorkSettings.ChangedUserPropertyName, StringComparison.OrdinalIgnoreCase)) { historyModel.ChangedUser = entry.OriginalValues[prop] != null ? entry.OriginalValues[prop].ToString() : "UNKNOWN"; } if (string.Equals(prop.Name, _unitOfWorkSettings.IdPropertyName, StringComparison.OrdinalIgnoreCase)) { historyModel.RowId = entry.OriginalValues[prop] != null ? entry.OriginalValues[prop].ToString() : "UNKNOWN"; } } historyModel.Hash = CryptoExtensions.Md5Encrypt(JsonConvert.SerializeObject(entry.Entity)); switch (entry.State) { case EntityState.Modified: foreach (var prop in entry.OriginalValues.Properties) { object currentValue = entry.CurrentValues[prop]; object originalValue = entry.OriginalValues[prop]; if (!object.Equals(currentValue, originalValue)) { historyModel.Columns.Add(new HistoryTableColumnsModel() { Name = prop.Name, OldValue = originalValue, NewValue = currentValue }); } } break; case EntityState.Deleted: foreach (var prop in entry.OriginalValues.Properties) { historyModel.Columns.Add(new HistoryTableColumnsModel() { Name = prop.Name, OldValue = entry.CurrentValues[prop] }); } break; } if (historyModel.Columns.Count > 0) { historyCollection.Add(historyModel); } } } return(historyCollection); }