using Microsoft.EntityFrameworkCore.Migrations; using System; #nullable disable namespace gehGassi.Persistence.Migrations { public partial class PublicWalkRequestWithNamesAndResponseStatusAdded : Migration { protected override void Up(MigrationBuilder migrationBuilder) { var viewName = "tv_PublicWalkRequestWithNamesAndResponseStatus"; var sql = @"CREATE FUNCTION tv_PublicWalkRequestWithNamesAndResponseStatus(@dogwalkerid nvarchar(128)) RETURNS TABLE AS RETURN SELECT PWR.*, CONCAT(DO.FirstName, ' ', DO.LastName) AS DogOwnerName, DO.Photo AS DogOwnerPhoto, D.Name As DogName, D.Photo AS DogPhoto, CONCAT(DW.FirstName, ' ', DW.LastName) AS DogWalkerName, DW.Photo AS DogWalkerPhoto, PWRE.Status AS ResponseStatus FROM PublicWalkRequests PWR LEFT JOIN AppUsers DO ON PWR.DogOwnerId = DO.Id LEFT JOIN Dogs D ON PWR.DogId = D.Id LEFT JOIN AppUsers DW ON PWR.DogWalkerId = DW.Id LEFT JOIN PublicWalkResponses PWRE ON PWRE.PublicWalkRequestId = PWR.Id AND PWRE.DogWalkerId = @dogwalkerid"; 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) { var viewName = "tv_PublicWalkRequestWithNames"; migrationBuilder.Sql($"IF(exists (SELECT 1 FROM sys.objects WHERE Name = '{viewName}')) BEGIN DROP FUNCTION [dbo].[{viewName}] END {Environment.NewLine} GO"); } } }