﻿CREATE PROCEDURE [session].[CreateMove]
	@To					VARCHAR(2),
	@From				VARCHAR(2),
	@IsPromotion		BIT,
	@PieceFromHand		NVARCHAR(13),
	@SessionName		[session].[SessionName]
AS

BEGIN
	SET NOCOUNT ON -- Performance boost
	SET XACT_ABORT ON -- Rollback transaction on error
	SET TRANSACTION ISOLATION LEVEL SNAPSHOT -- Ignores data changes that happen after the transaction begins.

	BEGIN TRANSACTION

		DECLARE @SessionId BIGINT = 0;
		SELECT @SessionId = Id
		FROM [session].[Session]
		WHERE [Name] = @SessionName;

		DECLARE @PieceIdFromhand INT = NULL;
		SELECT @PieceIdFromhand
		FROM [session].[Piece]
		WHERE [Name] = @PieceFromHand;

		INSERT INTO [session].[Move] 
		(SessionId, [To], [From], IsPromotion, PieceIdFromHand)
		VALUES
		(@SessionId, @To, @From, @IsPromotion, @PieceIdFromhand);

	COMMIT
END