:SetVar Procedure "BKRG::AddBook"
:r $(DropProcedureScript_AUX)
create procedure [$(Procedure)]
   @ID            uniqueidentifier = null out,
   @ISBN          $(dt_String_AUX) = null,
   @Name          $(dt_String_AUX) = null,
   @ReleaseYear   int = null,
   @Description   $(dt_String_AUX) = null,
   @AuthorId      uniqueidentifier,
   @PublisherId   uniqueidentifier,
   @SeriesId      uniqueidentifier,
   @CategoryId    uniqueidentifier,
   @GenreId       uniqueidentifier,
   @Status        uniqueidentifier  = null out,
   @Message       $(dt_Message_AUX) = null out
AS
/*==================================================================================================
# BookRegistry app. | Author: Sergei Y.Kitáev
----------------------------------------------------------------------------------------------------
#
# BOOK ADDING:
#
# === Usage: ===
# exec @RetValue = [BKRG::AddBook] ...,
#                  @Status=@Status out, @Message=@Message out
# exec @RetValue = [BKRG::AddBook] @ID = <Book ID or null> out,
#                  @Status=@Status out, @Message=@Message out
--------------------------------------------------------------------------------------------------*/
-- commstat_, commstat_, ...
:SetVar Status_AllreadyExists_ISBN "'A72713DF-A0B0-409C-8139-E8F657BA4279'"
--:SetVar Status_ "''"
/*================================================================================================*/
$(BEGIN_AUX)
----------------------------------------------------------------------------------------------------
select @Status=null, @Message=null
----------------------------------------------------------------------------------------------------
if @AuthorId is null or @PublisherId is null or @SeriesId is null or @CategoryId is null or @GenreId is null
   exec [AUX:RaiseIncorrectProcedureCall] N'$(Procedure)'
if @ID = $(guid_Empty_AUX)
   exec [AUX:RaiseIncorrectProcedureCall] N'$(Procedure)',
      'Empty GUID for @ID-parameter (book ID).'
----------------------------------------------------------------------------------------------------
set @ISBN = dbo.[BKRG:PrepareStrParam]( @ISBN )
if @ISBN is not null and exists(
   select * from [Books] (nolock) where [ISBN] = @ISBN )
begin
   select @Status=$(Status_AllreadyExists_ISBN), @Message=dbo.[AUX:LocalizedText](
      'Another book with such ISBN ('+@ISBN+') allready exists in the database.',
      N'Другая книга с тем же ISBN ('+@ISBN+') уже присутствует в базе данных.' )
   RETURN 1
end
-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if @ID is null set @ID = NEWID()

insert [Books] (
   [ID], [ISBN], [Name], [ReleaseYear], [Description],
   [AuthorId], [PublisherId], [SeriesId], [CategoryId], [GenreId] )
values (
   @ID,
   dbo.[BKRG:PrepareStrParam]( @ISBN ),
   dbo.[BKRG:PrepareStrParam]( @Name ),
   @ReleaseYear,
   dbo.[BKRG:PrepareStrParam]( @Description ),
   @AuthorId, @PublisherId, @SeriesId, @CategoryId, @GenreId )

----------------------------------------------------------------------------------------------------
$(END_AUX)
/*================================================================================================*/
GO
