Esempio n. 1
0
        public async Task <int> SaveImageAsync(Image image)
        {
            ImageDataVerification.VerifyImageParameteres(image);

            using (var connection = new SqlConnection(_connectionString))
            {
                await connection.OpenAsync();

                using (var transaction = await connection.BeginTransactionAsync())
                {
                    var affectedRows = await connection.ExecuteAsync(@"
                    INSERT INTO [dbo].[Images]
                               ([Id]
                               ,[Name]
                               ,[Description]
                               ,[Picture]
                               ,[DateCreated])
                         VALUES
                               (@Id
                               ,@Name
                               ,@Description
                               ,@Picture
                               ,@DateCreated)",
                                                                     new
                    {
                        image.Id,
                        image.Name,
                        image.Description,
                        image.Picture,
                        image.DateCreated
                    }, transaction : transaction);

                    affectedRows += await _tagDataAccess.InsertTagsAsync(image.Tags, transaction, connection);

                    transaction.Commit();

                    return(affectedRows);
                }
            }
        }
Esempio n. 2
0
        public async Task <int> UpdateImageAsync(Image image, bool updateTags)
        {
            ImageDataVerification.VerifyImageParameteres(image);

            using (var connection = new SqlConnection(_connectionString))
            {
                await connection.OpenAsync();

                using (var transaction = await connection.BeginTransactionAsync())
                {
                    var affectedRows = await connection.ExecuteAsync(@"UPDATE [dbo].[Images]
                        SET [Name] = @name,
                            [Description] = @description,
                            [Picture] = @picture,
                            [DateCreated] = @dateCreated
                        WHERE [Id] = @id",
                                                                     new
                    {
                        name        = image.Name,
                        description = image.Description,
                        picture     = image.Picture,
                        dateCreated = image.DateCreated,
                        id          = image.Id
                    }, transaction : transaction);

                    if (updateTags)
                    {
                        affectedRows += await _tagDataAccess.DeleteTagsByImageAsync(image.Id, transaction, connection);

                        affectedRows += await _tagDataAccess.InsertTagsAsync(image.Tags, transaction, connection);
                    }

                    transaction.Commit();

                    return(affectedRows);
                }
            }
        }