public void Insert(IMcAttachment entity)
        {
            using (var connection = _sqlWrapper.Connection())
            {
                var command = _sqlWrapper.Command($"MERGE INTO tblMcAttachments AS Target \r\n" +
                                                  $"USING (VALUES (@Id,@McAttachmentId,@MessageId,@FileHash,@FileName,@ContentType)) \r\n" +
                                                  $"AS Source ([Id],[McAttachmentId],[MessageId],[FileHash],[FileName],[ContentType]) \r\n" +
                                                  $"ON Target.[Id] = Source.[Id] \r\n" +
                                                  $"WHEN MATCHED THEN \r\n" +
                                                  $"UPDATE SET [McAttachmentId] = Source.[McAttachmentId],[MessageId] = Source.[MessageId],[FileHash] = Source.[FileHash],\r\n" +
                                                  $"[FileName] = Source.[FileName],[ContentType] = Source.[ContentType]\r\n" +
                                                  $"WHEN NOT MATCHED BY TARGET THEN \r\n" +
                                                  $"INSERT ([McAttachmentId],[MessageId],[FileHash],[FileName],[ContentType]) \r\n" +
                                                  $"VALUES ([McAttachmentId],[MessageId],[FileHash],[FileName],[ContentType]);\r\n", connection);

                command.Parameters.Add(new SqlParameter("@Id", null));
                command.Parameters.Add(new SqlParameter("@McAttachmentId", entity.Id));
                command.Parameters.Add(new SqlParameter("@MessageId", entity.MessageId));
                command.Parameters.Add(new SqlParameter("@FileHash", entity.FileHash));
                command.Parameters.Add(new SqlParameter("@FileName", entity.FileName));
                command.Parameters.Add(new SqlParameter("@ContentType", entity.ContentType));


                connection.Open();
                command.ExecuteNonQuery();
            }
        }
Beispiel #2
0
        public void WhenIInsertANewAttachmentTheCommandIsRecieved()
        {
            //Arranging
            ISqlWrapper   sqlWrapper = Substitute.For <ISqlWrapper>();
            IMcAttachment payload    = new McAttachment();

            _target = new McAttachmentRepository(sqlWrapper);

            //Act
            _target.Insert(payload);

            //Assert
            sqlWrapper.Connection().Received();
            sqlWrapper.Command().Received();
        }