private static async Task <bool> AddAlbumImageMap( AlbumImageMap albumImageMap, ulong lastInsertedImageId, IDbConnection conn, IDbTransaction trans) { var paramInsertAlbumImageMap = new DynamicParameters(); paramInsertAlbumImageMap.Add(QueryParameters.AlbumId, albumImageMap.AlbumId); paramInsertAlbumImageMap.Add(QueryParameters.ImageId, lastInsertedImageId); paramInsertAlbumImageMap.Add(QueryParameters.OriginalFileName, albumImageMap.OriginalFileName); try { await conn.ExecuteAsync( Queries.InsertAlbumImageMap, paramInsertAlbumImageMap, trans); } catch (MySqlException e) { if (e.Number != 1062) { throw e; } } var paramSelectAlbumImageMap = new DynamicParameters(); paramSelectAlbumImageMap.Add(QueryParameters.AlbumId, albumImageMap.AlbumId); paramSelectAlbumImageMap.Add(QueryParameters.ImageId, lastInsertedImageId); var insertedAlbumImageMap = await conn.QuerySingleAsync <AlbumImageMap>( Queries.GetAlbumImageMap, paramSelectAlbumImageMap, trans); // ReSharper disable once InvertIf if (insertedAlbumImageMap == null || insertedAlbumImageMap.OriginalFileName != albumImageMap.OriginalFileName) { Console.WriteLine($"Transaction is rolled back because album_image_map " + $"record was not inserted correctly."); return(false); } albumImageMap.ImageId = lastInsertedImageId; return(true); }
private static async Task <bool> SetQueuedImageStatus( AlbumImageMap albumImageMap, ulong queuedImageId, IDbConnection conn, IDbTransaction trans) { var paramSetStatusQueuedImage = new DynamicParameters(); paramSetStatusQueuedImage.Add(QueryParameters.QueuedImageId, queuedImageId); paramSetStatusQueuedImage.Add(QueryParameters.Status, QueuedImage.StatusCompleted); var numberOfModifiedQueuedImageRecords = await conn.ExecuteAsync( Queries.SetQueuedImageStatus, paramSetStatusQueuedImage, trans); if (numberOfModifiedQueuedImageRecords != 1) { Console.WriteLine($"Transaction is rolled back because number of inserted " + $"album_image_map records was not 1."); return(false); } var paramSelectQueuedImage = new DynamicParameters(); paramSelectQueuedImage.Add(QueryParameters.QueuedImageId, queuedImageId); var insertedQueuedImage = await conn.QuerySingleAsync <QueuedImage>( Queries.GetQueuedImageByQueuedImageId, paramSelectQueuedImage, trans); // ReSharper disable once InvertIf if (insertedQueuedImage == null || insertedQueuedImage.Status != QueuedImage.StatusCompleted) { Console.WriteLine($"Transaction is rolled back because album_image_map " + $"record was not inserted correctly."); return(false); } return(numberOfModifiedQueuedImageRecords == 1); }
public async Task <bool> AddImageToAlbum(Image image, AlbumImageMap albumImageMap, List <int> sizes, Dictionary <int, ImageFile> files, ulong queuedImageId) { if (sizes.Count != 6 || sizes.First() != 0) { Console.WriteLine("There should be exactly 6 values for sizes, and the first should be 0"); return(false); } if (sizes.Any(x => !files.Keys.Contains(x)) || files.Keys.Any(x => !sizes.Contains(x))) { // TODO: If condition does not capture the "EXACTLY 1 file per size" condition Console.WriteLine("There should be exactly 1 file per size, and at least one size per file"); return(false); } using (var conn = new MySqlConnection(_connectionString)) { await conn.OpenAsync(); using (var trans = conn.BeginTransaction()) { try { var imageFileIdPerSize = await AddImageFiles(files, conn, trans); if (imageFileIdPerSize == null) { trans.Rollback(); return(false); } image.OriginalImageFileId = imageFileIdPerSize[sizes[0]]; image.FullSizeImageFileId = imageFileIdPerSize[sizes[1]]; image.LargeImageFileId = imageFileIdPerSize[sizes[2]]; image.MediumImageFileId = imageFileIdPerSize[sizes[3]]; image.SmallImageFileId = imageFileIdPerSize[sizes[4]]; image.IconImageFileId = imageFileIdPerSize[sizes[5]]; var lastInsertedImageId = await AddImage(image, conn, trans); if (lastInsertedImageId == null) { trans.Rollback(); return(false); } if (!await AddAlbumImageMap( albumImageMap, lastInsertedImageId.GetValueOrDefault(), conn, trans)) { trans.Rollback(); return(false); } if (!await SetQueuedImageStatus(albumImageMap, queuedImageId, conn, trans)) { trans.Rollback(); return(false); } trans.Commit(); return(true); } catch (Exception e) { Console.WriteLine("Error:" + e); trans.Rollback(); return(false); } } } }