Created
January 7, 2011 11:31
-
-
Save dariuszparys/769374 to your computer and use it in GitHub Desktop.
WebMatrix Gästebuch Helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@functions | |
{ | |
public const string DatabaseName = "[DATENBANK-NAME-HIER-EINGEBEN]"; | |
public const string TableGuestbook = "Guestbook"; | |
public static string Result = string.Empty; | |
public static void InitializeDatabase() | |
{ | |
var database = Database.Open(DatabaseName); | |
if( !CheckIfTableExists(database, TableGuestbook) ) | |
{ | |
CreateGuestbookTable(database); | |
} | |
database.Close(); | |
} | |
public static void CreateEntryFromPost() | |
{ | |
if (IsPost) | |
{ | |
var name = Request["name"]; | |
if (name.IsEmpty()) | |
{ | |
ModelState.AddError("name", "Du musst deinen Namen angeben."); | |
} | |
var entry = Request["entry"]; | |
if (entry.IsEmpty()) | |
{ | |
ModelState.AddError("entry", "Du musst etwas schreiben."); | |
} | |
if (ModelState.IsValid) | |
{ | |
var database = Database.Open(DatabaseName); | |
var insert = string.Format( "INSERT INTO {0} (Name, Entry, Created) VALUES (@0, @1, @2)", TableGuestbook ); | |
database.Execute(insert, name, entry, DateTime.Now); | |
database.Close(); | |
} | |
} | |
} | |
private static bool CheckIfTableExists(Database database, string tableName) | |
{ | |
var result = database.QuerySingle("SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = @0", tableName); | |
return result != null; | |
} | |
private static void CreateGuestbookTable(Database database) | |
{ | |
string table = string.Format("CREATE TABLE {0} ( [Name] nvarchar(100) not null, [Entry] nvarchar(4000) not null, [Created] datetime not null)", TableGuestbook); | |
database.Execute(table, new object[0]); | |
} | |
} | |
@helper ShowEntries( int numberOfEntries = 0 ) | |
{ | |
var query = string.Empty; | |
if( numberOfEntries > 0 ) | |
{ | |
query = string.Format("SELECT TOP {0} * FROM {1} ORDER BY Created DESC", numberOfEntries.ToString(), GuestbookHelper.TableGuestbook); | |
} | |
else | |
{ | |
query = string.Format("SELECT * FROM {0} ORDER BY Created DESC", GuestbookHelper.TableGuestbook); | |
} | |
var database = Database.Open(DatabaseName); | |
var entries = database.Query(query); | |
<ul> | |
@foreach( var entry in entries) | |
{ | |
<li> | |
<div class="guestbook-entry-from">Von @entry.Name</div> | |
<div class="guestbook-entry-created">eintragen am @entry.Created.ToShortDateString()</div> | |
<div class="guestbook-entry-entry">@entry.Entry</div> | |
</li> | |
} | |
</ul> | |
} | |
@helper ShowForm() | |
{ | |
<form method="post"> | |
<fieldset> | |
<legend>Ins Gästebuch eintragen</legend> | |
<div class="guestbook-form-from"> | |
<label for="name">Dein Name:</label> | |
<input id="name" type="text" name="name" size="30"/> | |
</div> | |
<div class="guestbook-form-entry"> | |
<label for="entry">Eintrag:</label> | |
<textarea id="entry" name="entry" cols="50" rows="5"></textarea> | |
</div> | |
<input type="submit" value="Eintragen"/> | |
</fieldset> | |
</form> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment