1+ using System . Collections . Generic ;
2+ using System . Linq ;
3+ using WebExpress . Tutorial . WebUI . Model ;
4+ using WebExpress . Tutorial . WebUI . WWW . Controls ;
5+ using WebExpress . WebApp . WebRestApi ;
6+ using WebExpress . WebCore . WebApplication ;
7+ using WebExpress . WebCore . WebAttribute ;
8+ using WebExpress . WebCore . WebMessage ;
9+ using WebExpress . WebCore . WebRestApi ;
10+ using WebExpress . WebCore . WebSitemap ;
11+
12+ namespace WebExpress . Tutorial . WebUI . WWW . Api . _1
13+ {
14+ /// <summary>
15+ /// Represents a REST API table for managing and retrieving data about Monkey Island games.
16+ /// </summary>
17+ [ Title ( "Monkey Island Games" ) ]
18+ [ Method ( CrudMethod . GET ) ]
19+ [ Method ( CrudMethod . DELETE ) ]
20+ [ Method ( CrudMethod . PUT ) ]
21+ public sealed class MonkeyIslandGames : RestApiCrudList < Game >
22+ {
23+ private readonly string _formUri ;
24+
25+ /// <summary>
26+ /// Initializes a new instance of the class.
27+ /// </summary>
28+ /// <param name="sitemapManager">The sitemap manager used to retrieve URIs for the application context.</param>
29+ /// <param name="applicationContext">The application context containing the current state of the application.</param>
30+ public MonkeyIslandGames ( ISitemapManager sitemapManager , IApplicationContext applicationContext )
31+ {
32+ var uri = sitemapManager . GetUri < RestList > ( applicationContext ) ;
33+ _formUri = uri ? . SetFragment ( "myListForm" ) ? . ToString ( ) ;
34+
35+ Data = ViewModel . MonkeyIslandGames ;
36+ }
37+
38+ /// <summary>
39+ /// Retrieves a collection of options.
40+ /// </summary>
41+ public override IEnumerable < RestApiCrudOption > GetOptions ( Request request , Game row )
42+ {
43+ yield return new RestApiCrudOptionHeader ( request )
44+ {
45+ Label = "webexpress.webapp:header.setting.label"
46+ } ;
47+
48+ yield return new RestApiCrudOptionEdit ( request )
49+ {
50+ Uri = _formUri
51+ } ;
52+
53+ yield return new RestApiCrudOptionSeperator ( request ) ;
54+ yield return new RestApiCrudOptionDelete ( request ) ;
55+ }
56+
57+ /// <summary>
58+ /// Retrieves a collection of games based on the specified filter.
59+ /// </summary>
60+ public override IEnumerable < Game > GetData ( string filter , Request request )
61+ {
62+ if ( string . IsNullOrEmpty ( filter ) || filter == "null" )
63+ {
64+ return Data ;
65+ }
66+
67+ return Data . Where ( x => x . Name . Contains ( filter ) ) ;
68+ }
69+
70+ /// <summary>
71+ /// Performs validation before updating game data.
72+ /// </summary>
73+ public override RestApiValidationResult ValidateUpdateData ( Game item , Request request )
74+ {
75+ return new RestApiValidator ( request )
76+ . Require ( nameof ( Game . Name ) )
77+ . MinLength ( nameof ( Game . Name ) , 3 )
78+ . Require ( nameof ( Game . ReleaseYear ) )
79+ . Result ;
80+ }
81+
82+ /// <summary>
83+ /// Updates the game data record.
84+ /// </summary>
85+ public override void UpdateData ( Game item , Request request )
86+ {
87+ item . Name = request . GetParameter ( nameof ( Game . Name ) ) ? . Value ;
88+ item . Description = request . GetParameter ( nameof ( Game . Description ) ) ? . Value ;
89+ item . ReleaseYear = int . TryParse ( request . GetParameter ( nameof ( Game . ReleaseYear ) ) ? . Value , out var year ) ? year : 0 ;
90+ }
91+
92+ /// <summary>
93+ /// Deletes a game entry.
94+ /// </summary>
95+ public override void DeleteData ( string id , Request request )
96+ {
97+ ViewModel . MonkeyIslandGames . RemoveAll ( x => x . Id . ToString ( ) == id ) ;
98+ }
99+ }
100+ }
0 commit comments