Ejemplo n.º 1
0
 public static void CopyTo(this NoteContentEntity noteContentEntity, NoteContent targetNoteContent)
 {
     targetNoteContent.Id          = noteContentEntity.Id;
     targetNoteContent.NoteId      = noteContentEntity.NoteId;
     targetNoteContent.Type        = noteContentEntity.Type;
     targetNoteContent.Content     = noteContentEntity.Content;
     targetNoteContent.CreateTime  = noteContentEntity.CreateTime;
     targetNoteContent.IsDeleted   = noteContentEntity.IsDeleted;
     targetNoteContent.DeletedTime = noteContentEntity.DeletedTime;
 }
Ejemplo n.º 2
0
        public NoteContent AddNoteContent(NoteContent noteContent)
        {
            NoteContentEntity entity = noteContent.ToEntity();

            entity.CreateTime  = DateTime.Now;
            entity.DeletedTime = DateTime.Now;
            entity.IsDeleted   = false;
            this.dbContext.NoteContents.Add(entity);
            this.dbContext.SaveChanges();
            return(entity.ToModel());
        }
Ejemplo n.º 3
0
        public List <NoteContent> GetNoteCommentByNoteId(int id)
        {
            try
            {
                if (id <= 0)
                {
                    return(null);
                }

                DbParameter[] parameters = new DbParameter[]
                {
                    new SqlParameter()
                    {
                        ParameterName = SpParamsOfNoteContent.Sp_Select_NoteContentByNoteId_NoteId,
                        Value         = id
                    }
                };

                using (DataSet dataSet = DbHelper.Instance.RunProcedureGetDataSet(SpNamesOfNoteContent.Sp_Select_NoteContentByNoteId, parameters))
                {
                    NoteContentEntity  entity       = null;
                    List <NoteContent> noteContents = new List <NoteContent>();

                    if (dataSet != null && dataSet.Tables != null && dataSet.Tables.Count != 0 && dataSet.Tables[0].Rows != null && dataSet.Tables[0].Rows.Count != 0)
                    {
                        DataRowCollection    dataRows    = dataSet.Tables[0].Rows;
                        DataColumnCollection dataColumns = dataSet.Tables[0].Columns;

                        foreach (DataRow dataRow in dataRows)
                        {
                            entity = new NoteContentEntity()
                            {
                                Id          = (int)dataRow[dataColumns[0]],
                                NoteId      = (int)dataRow[dataColumns[1]],
                                Type        = (int)dataRow[dataColumns[2]],
                                Content     = dataRow[dataColumns[3]].ToString(),
                                CreateTime  = (DateTime)dataRow[dataColumns[4]],
                                IsDeleted   = (bool)dataRow[dataColumns[5]],
                                DeletedTime = (DateTime)dataRow[dataColumns[6]]
                            };

                            noteContents.Add(entity.ToModel());
                        }
                    }

                    return(noteContents);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Ejemplo n.º 4
0
 public static NoteContent ToModel(this NoteContentEntity entity)
 {
     return(new NoteContent()
     {
         Id = entity.Id,
         NoteId = entity.NoteId,
         Type = entity.Type,
         Content = entity.Content,
         CreateTime = entity.CreateTime,
         IsDeleted = entity.IsDeleted,
         DeletedTime = entity.DeletedTime
     });
 }
Ejemplo n.º 5
0
        public bool AddNoteAndContents(Note note, List <NoteContent> noteContents)
        {
            using (DbContextTransaction tran = this.dbContext.Database.BeginTransaction())
            {
                try
                {
                    NoteEntity noteEntity = note.ToEntity();
                    noteEntity.CreateTime      = DateTime.Now;
                    noteEntity.DeletedTime     = DateTime.Now;
                    noteEntity.IsDeleted       = false;
                    noteEntity.LastBrowsedTime = DateTime.Now;
                    this.dbContext.Notes.Add(noteEntity);
                    this.dbContext.SaveChanges();
                    noteEntity.CopyTo(note);

                    List <NoteContentEntity> noteContentEntities = new List <NoteContentEntity>();

                    foreach (NoteContent nc in noteContents)
                    {
                        NoteContentEntity entity = nc.ToEntity();
                        entity.CreateTime  = DateTime.Now;
                        entity.DeletedTime = DateTime.Now;
                        entity.IsDeleted   = false;
                        entity.NoteId      = noteEntity.Id;
                        this.dbContext.NoteContents.Add(entity);
                        noteContentEntities.Add(entity);
                    }

                    this.dbContext.SaveChanges();
                    tran.Commit();

                    for (int i = 0; i < noteContents.Count; i++)
                    {
                        noteContentEntities[i].CopyTo(noteContents[i]);
                    }
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    throw ex;
                }
            }

            return(true);
        }
Ejemplo n.º 6
0
        public NoteContent AddNoteContent(NoteContent noteContent)
        {
            try
            {
                if (noteContent == null)
                {
                    return(null);
                }

                DbParameter[] parameters = new DbParameter[]
                {
                    new SqlParameter()
                    {
                        ParameterName = SpParamsOfNoteContent.Sp_Insert_NoteContent_NoteId,
                        Value         = noteContent.NoteId
                    },
                    new SqlParameter()
                    {
                        ParameterName = SpParamsOfNoteContent.Sp_Insert_NoteContent_Type,
                        Value         = noteContent.Type
                    },
                    new SqlParameter()
                    {
                        ParameterName = SpParamsOfNoteContent.Sp_Insert_NoteContent_Content,
                        Value         = noteContent.Content
                    }
                };

                using (DataSet dataSet = DbHelper.Instance.RunProcedureGetDataSet(SpNamesOfNoteContent.Sp_Insert_NoteContent, parameters))
                {
                    NoteContentEntity entity = null;

                    if (dataSet != null && dataSet.Tables != null && dataSet.Tables.Count != 0 && dataSet.Tables[0].Rows != null && dataSet.Tables[0].Rows.Count != 0)
                    {
                        DataRow dataRow = dataSet.Tables[0].Rows[0];
                        DataColumnCollection dataColumns = dataSet.Tables[0].Columns;

                        entity = new NoteContentEntity()
                        {
                            Id          = (int)dataRow[dataColumns[0]],
                            NoteId      = (int)dataRow[dataColumns[1]],
                            Type        = (int)dataRow[dataColumns[2]],
                            Content     = dataRow[dataColumns[3]].ToString(),
                            CreateTime  = (DateTime)dataRow[dataColumns[4]],
                            IsDeleted   = (bool)dataRow[dataColumns[5]],
                            DeletedTime = (DateTime)dataRow[dataColumns[6]]
                        };
                    }

                    if (entity == null)
                    {
                        return(null);
                    }

                    return(entity.ToModel());
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Ejemplo n.º 7
0
        public bool AddNoteAndContents(Note note, List <NoteContent> noteContents)
        {
            try
            {
                using (DbConnection connection = DbHelper.Instance.Provider.CreateConnection())
                {
                    connection.ConnectionString = DbHelper.Instance.ConnectionString;
                    connection.Open();

                    using (DbTransaction trans = connection.BeginTransaction())
                    {
                        try
                        {
                            using (DbDataAdapter sqlDA = DbHelper.Instance.Provider.CreateDataAdapter())
                            {
                                #region 获取添加成功后返回的NoteEntity,包括生产的ID

                                DbParameter[] parameters = new DbParameter[]
                                {
                                    new SqlParameter()
                                    {
                                        ParameterName = SpParamsOfNote.Sp_Insert_Note_AuthorId,
                                        Value         = note.AuthorId
                                    },
                                    new SqlParameter()
                                    {
                                        ParameterName = SpParamsOfNote.Sp_Insert_Note_Title,
                                        Value         = note.Title
                                    },
                                    new SqlParameter()
                                    {
                                        ParameterName = SpParamsOfNote.Sp_Insert_Note_IsShared,
                                        Value         = note.IsShared
                                    },
                                    new SqlParameter()
                                    {
                                        ParameterName = SpParamsOfNote.Sp_Insert_Note_Remark,
                                        Value         = note.Remark
                                    }
                                };

                                Note returnNote = null;

                                using (DataSet dataSet = new DataSet())
                                {
                                    sqlDA.SelectCommand             = DbHelper.Instance.BuildQueryCommand(connection, SpNamesOfNote.Sp_Insert_Note, parameters);
                                    sqlDA.SelectCommand.Transaction = trans;
                                    sqlDA.Fill(dataSet);
                                    sqlDA.SelectCommand.Parameters.Clear();


                                    if (dataSet != null && dataSet.Tables != null && dataSet.Tables.Count != 0 && dataSet.Tables[0].Rows != null && dataSet.Tables[0].Rows.Count != 0)
                                    {
                                        DataRow dataRow = dataSet.Tables[0].Rows[0];
                                        DataColumnCollection dataColumns = dataSet.Tables[0].Columns;

                                        NoteEntity noteEntity = new NoteEntity()
                                        {
                                            Id              = (int)dataRow[dataColumns[0]],
                                            AuthorId        = (int)dataRow[dataColumns[1]],
                                            Title           = dataRow[dataColumns[2]].ToString(),
                                            Remark          = dataRow[dataColumns[3]].ToString(),
                                            CreateTime      = (DateTime)dataRow[dataColumns[4]],
                                            LastBrowsedTime = (DateTime)dataRow[dataColumns[5]],
                                            IsShared        = (bool)dataRow[dataColumns[6]],
                                            IsDeleted       = (bool)dataRow[dataColumns[7]],
                                            DeletedTime     = (DateTime)dataRow[dataColumns[8]]
                                        };

                                        returnNote = noteEntity.ToModel();
                                        returnNote.BrowsedTimes  = (int)dataRow[dataColumns[9]];
                                        returnNote.ApprovedTimes = (int)dataRow[dataColumns[10]];
                                        returnNote.CommentCount  = (int)dataRow[dataColumns[11]];
                                    }
                                    else
                                    {
                                        // 添加失败,回滚事务
                                        trans.Rollback();
                                        return(false);
                                    }
                                }

                                #endregion

                                using (DataSet dataSet = new DataSet())
                                {
                                    #region 添加NoteContent

                                    if (noteContents != null)
                                    {
                                        List <NoteContentEntity> noteContentEntities = new List <NoteContentEntity>();

                                        foreach (NoteContent noteContent in noteContents)
                                        {
                                            parameters = new DbParameter[]
                                            {
                                                new SqlParameter()
                                                {
                                                    ParameterName = SpParamsOfNoteContent.Sp_Insert_NoteContent_NoteId,
                                                    Value         = returnNote.Id
                                                },
                                                new SqlParameter()
                                                {
                                                    ParameterName = SpParamsOfNoteContent.Sp_Insert_NoteContent_Type,
                                                    Value         = noteContent.Type
                                                },
                                                new SqlParameter()
                                                {
                                                    ParameterName = SpParamsOfNoteContent.Sp_Insert_NoteContent_Content,
                                                    Value         = noteContent.Content
                                                }
                                            };

                                            sqlDA.SelectCommand             = DbHelper.Instance.BuildQueryCommand(connection, SpNamesOfNoteContent.Sp_Insert_NoteContent, parameters);
                                            sqlDA.SelectCommand.Transaction = trans;
                                            sqlDA.Fill(dataSet);
                                            sqlDA.SelectCommand.Parameters.Clear();
                                            NoteContentEntity noteContentEntity = null;

                                            if (dataSet != null && dataSet.Tables != null && dataSet.Tables.Count != 0 && dataSet.Tables[0].Rows != null && dataSet.Tables[0].Rows.Count != 0)
                                            {
                                                DataRow dataRow = dataSet.Tables[0].Rows[0];
                                                DataColumnCollection dataColumns = dataSet.Tables[0].Columns;

                                                noteContentEntity = new NoteContentEntity()
                                                {
                                                    Id          = (int)dataRow[dataColumns[0]],
                                                    NoteId      = (int)dataRow[dataColumns[1]],
                                                    Type        = (int)dataRow[dataColumns[2]],
                                                    Content     = dataRow[dataColumns[3]].ToString(),
                                                    CreateTime  = (DateTime)dataRow[dataColumns[4]],
                                                    IsDeleted   = (bool)dataRow[dataColumns[5]],
                                                    DeletedTime = (DateTime)dataRow[dataColumns[6]]
                                                };

                                                noteContentEntities.Add(noteContentEntity);
                                            }
                                            else
                                            {
                                                // 添加失败,回滚事务
                                                trans.Rollback();
                                                return(false);
                                            }

                                            dataSet.Clear();
                                        }

                                        returnNote.CopyTo(note);

                                        for (int i = 0; i < noteContentEntities.Count; i++)
                                        {
                                            noteContentEntities[i].CopyTo(noteContents[i]);
                                        }
                                    }

                                    #endregion
                                }
                            }

                            trans.Commit();
                            return(true);
                        }
                        catch (Exception ex)
                        {
                            trans.Rollback();
                            throw ex;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Ejemplo n.º 8
0
        public NoteContent GetNoteCommentById(int id)
        {
            NoteContentEntity entity = this.dbContext.NoteContents.FirstOrDefault(nc => nc.Id == id);

            return(entity == null ? null : entity.ToModel());
        }