gehgassi_backend/gehGassi.Persistence/Migrations/20220822170201_AdvertisementLocation.cs

82 lines
4.4 KiB
C#

using Microsoft.EntityFrameworkCore.Migrations;
using NetTopologySuite.Geometries;
using System;
#nullable disable
namespace gehGassi.Persistence.Migrations
{
public partial class AdvertisementLocation : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "Lat",
table: "Advertisements");
migrationBuilder.DropColumn(
name: "Lng",
table: "Advertisements");
migrationBuilder.AddColumn<Point>(
name: "Location",
table: "Advertisements",
type: "geography",
nullable: true);
var viewName = "tv_AdvertisementsWithNames";
var sql = @"CREATE FUNCTION tv_AdvertisementsWithNames (@language nvarchar(2), @fallback nvarchar(2)) RETURNS TABLE AS RETURN
SELECT dbo.Advertisements.*,
C.Name AS CustomerName,
AC.Name AS AdvertisementCategoryName,
Isnull(JSON_VALUE(dbo.Advertisements.NameLocalized, '$.' + @language), JSON_VALUE(dbo.Advertisements.NameLocalized, '$.' + @fallback)) AS Name,
Isnull(JSON_VALUE(dbo.Advertisements.DescriptionLocalized, '$.' + @language), JSON_VALUE(dbo.Advertisements.DescriptionLocalized, '$.' + @fallback)) AS Description,
JSON_VALUE(dbo.Advertisements.ImageLanguageLocalized, '$.' + @language) AS ImageLanguage,
JSON_VALUE(dbo.Advertisements.UrlLanguageLocalized, '$.' + @language) AS UrlLanguage
FROM dbo.Advertisements
LEFT JOIN dbo.tv_CustomersWithNames(@language, @fallback) C ON dbo.Advertisements.CustomerId = C.Id
LEFT JOIN dbo.tv_AdvertisementCategoryWithNames(@language, @fallback) AC ON dbo.Advertisements.AdvertisementCategoryId = AC.Id";
migrationBuilder.Sql($"IF(exists (SELECT 1 FROM sys.objects WHERE Name = '{viewName}')) BEGIN DROP FUNCTION [dbo].[{viewName}] END {Environment.NewLine} GO");
migrationBuilder.Sql($"{sql} {Environment.NewLine} GO");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "Location",
table: "Advertisements");
migrationBuilder.AddColumn<double>(
name: "Lat",
table: "Advertisements",
type: "float",
nullable: false,
defaultValue: 0.0);
migrationBuilder.AddColumn<double>(
name: "Lng",
table: "Advertisements",
type: "float",
nullable: false,
defaultValue: 0.0);
var viewName = "tv_AdvertisementsWithNames";
var sql = @"CREATE FUNCTION tv_AdvertisementsWithNames (@language nvarchar(2), @fallback nvarchar(2)) RETURNS TABLE AS RETURN
SELECT dbo.Advertisements.*,
C.Name AS CustomerName,
AC.Name AS AdvertisementCategoryName,
Isnull(JSON_VALUE(dbo.Advertisements.NameLocalized, '$.' + @language), JSON_VALUE(dbo.Advertisements.NameLocalized, '$.' + @fallback)) AS Name,
Isnull(JSON_VALUE(dbo.Advertisements.DescriptionLocalized, '$.' + @language), JSON_VALUE(dbo.Advertisements.DescriptionLocalized, '$.' + @fallback)) AS Description,
JSON_VALUE(dbo.Advertisements.ImageLanguageLocalized, '$.' + @language) AS ImageLanguage,
JSON_VALUE(dbo.Advertisements.UrlLanguageLocalized, '$.' + @language) AS UrlLanguage
FROM dbo.Advertisements
LEFT JOIN dbo.tv_CustomersWithNames(@language, @fallback) C ON dbo.Advertisements.CustomerId = C.Id
LEFT JOIN dbo.tv_AdvertisementCategoryWithNames(@language, @fallback) AC ON dbo.Advertisements.AdvertisementCategoryId = AC.Id";
migrationBuilder.Sql($"IF(exists (SELECT 1 FROM sys.objects WHERE Name = '{viewName}')) BEGIN DROP FUNCTION [dbo].[{viewName}] END {Environment.NewLine} GO");
migrationBuilder.Sql($"{sql} {Environment.NewLine} GO");
}
}
}