Exemplo n.º 1
0
        /// <summary>
        /// Replaces a venue's set of tags with a new set
        /// </summary>
        /// <returns>Returns a result indicating if the delete succeeded</returns>
        public async Task <Result> ReplaceVenueTags(int venueId, List <string> tagNames)
        {
            if (tagNames.Count == 0)
            {
                return(Result.Ok());
            }

            try
            {
                using var con = new Npgsql.NpgsqlConnection(settings.Connection.DatabaseConnectionString);
                await con.OpenAsync().ConfigureAwait(false);

                var tran = await con.BeginTransactionAsync().ConfigureAwait(false);

                var tags = await con.QueryAsync <Tag>("SELECT * FROM \"Tag\" WHERE tagname = ANY(@TagNames)", new { TagNames = tagNames }).ConfigureAwait(false);

                await con.ExecuteAsync("DELETE FROM \"VenueTag\" WHERE venueId = @VenueId", new { VenueId = venueId }).ConfigureAwait(false);

                var venueTags = tags.Select(t => new VenueTag {
                    VenueId = venueId, TagId = t.TagId
                }).ToList();

                await con.ExecuteAsync("INSERT INTO \"VenueTag\" (venueid, tagid) VALUES (@VenueId, @TagId)", venueTags).ConfigureAwait(false);

                try {
                    await tran.CommitAsync().ConfigureAwait(false);
                }
                catch (Exception ex)
                {
                    await tran.RollbackAsync().ConfigureAwait(false);

                    return(Result.Fail(ex.ToString()));
                }

                return(Result.Ok());
            }
            catch (Exception ex)
            {
                return(Result.Fail(ex.ToString()));
            }
        }