I have a database with some random data like:
Book:
Id uniqueidentifier,
title nvarchar(255),
author nvarchar(255),
genre nvarchar (255),
deleted datetime
I have many different books in my Book table. When I want to delete a book, I dont really delete it from database, but just set datetime for deleted, when I delete it.
Here is a case that bothers me.
If user tries to look (search) for a book he deleted (which doesn't exist anymore), it needs to return empty array
Here is what I've done, but it doesn't work:
CREATE PROCEDURE dbo.Book_GetById
@id uniqueidentifier
AS
BEGIN
DECLARE @_deleted DATETIME
SET @_deleted = NULL
SELECT @_deleted = Deleted
FROM Book
WHERE Id = @id
IF (@_deleted IS NOT NULL)
BEGIN
SELECT JSON_QUERY('[]')
FOR JSON PATH
This is not the entire procedure, just the part where I want to return an empty JSON array by checking if deleted is not null, it means that there is a datetime set for that row - book deleted (if it is null, it is not deleted).
PS1: if there is any other better way than empty array, id like to hear about it too!
PS2: I need to "return" data in my unit testing to see if book is really deleted by checking Assert.IsNotNull(book.Deleted);
When I run my debugger in unit testing and when I step over, my var books says null, which I think isn't really right I guess