Moved dynamic constraints in a dedicated folder

This commit is contained in:
Daniel Hutzel
2025-07-24 10:38:43 +02:00
parent c9021d0f46
commit 65eccfad33
7 changed files with 36 additions and 8 deletions

View File

@@ -1,2 +1,2 @@
using { AdminService } from './admin-service';
annotate AdminService with @requires: false; //'admin';
annotate AdminService with @requires:'admin';

View File

@@ -1,6 +1,6 @@
using { sap.capire.bookshop as my } from '../db/schema';
service AdminService @(path:'/admin') {
entity Authors as projection on my.Authors excluding { books};
entity Authors as projection on my.Authors;
entity Books as projection on my.Books;
entity Genres as projection on my.Genres;
}

View File

@@ -1,10 +0,0 @@
namespace sap.capire.bookshop;
using from '../db/schema';
view Books.field.control as select from Books { ID,
genre.name == 'Drama' ? 'readonly' :
null as price
}
extend Books with {
fc : Association to Books.field.control on fc.ID = $self.ID
}

View File

@@ -1,76 +0,0 @@
using { AdminService, sap.capire.bookshop as my } from './admin-service';
extend service AdminService with {
// entity Books.drafts as projection on AdminService.Books;
// @cds.api.ignore view Books.drafts.constraints as select from AdminService.Books.drafts mixin {
// before: Association to my.Books on before.ID = $self.ID;
// base: Association to my.Books on base.ID = $self.ID;
// } into { ID, // FIXME: compiler should resolve Books without AdminService prefix
// case
// when title is null then 'is missing'
// when trim(title)='' then 'must not be empty'
// end as title,
// ...
// }
/**
* Validation constraints for Books
*/
@cds.api.ignore view Books.constraints as select from AdminService.Books mixin {
base: Association to my.Books on base.ID = $self.ID;
} into { ID, // FIXME: compiler should resolve Books without AdminService prefix
// two-step mandatory check
case
when title is null then 'is missing'
when trim(title)='' then 'must not be empty'
end as title,
// the above is equivalent to:
// title is null ? 'is missing' : trim(title)='' ? 'must not be empty' :
// range check
stock < 0 ? 'must not be negative' :
null as stock,
// range check
price < 0 ? 'must not be negative' :
null as price,
// assert target check
genre.ID is not null and not exists genre ? 'does not exist' :
null as genre,
// multiple constraints: mandatory + assert target + special
author.ID is null ? 'is missing' : // FIXME: 1) // TODO: 2)
not exists author ? 'Author does not exist: ' || author.ID :
count(base.author.books.ID) -1 > 1 ? author.name || ' already wrote too many books' : // TODO: 3)
null as author,
} group by ID;
// 1) FIXME: expected author.ID to refer to foreign key,
// apparently that is not the case -> move one line up
// and run test to see the erroneous impact.
// 2) TODO: we should allow to write author is null instead of author.ID is null
// 3) TODO: we should support count(author.books)
/**
* Validation constraints for Authors
*/
view Authors.constraints as select from AdminService.Authors { ID, // FIXME: compiler should resolve Authors without AdminService prefix
// two-step mandatory check
name = null ? 'is missing' : trim(name)='' ? 'must not be empty' :
null as name,
// constraint related to two fields
dateOfDeath > dateOfBirth ? 'we must be born before we die' : null as _born_before_death,
$self._born_before_death as dateOfBirth,
$self._born_before_death as dateOfDeath,
}
}