レガシーASPでMVCフレームワーク

#概要
レガシーASPでpeetan.aspを導入して、CRUDを確認した。

#写真
![p.JPG](https://qiita-image-store.s3.amazonaws.com/0/18104/f58e194a-c614-1ba3-b9ea-bdf95cbe323d.jpeg)


#peetanの設置
ダウンロード
https://ja.osdn.net/projects/peetan/releases/27870

config.asp

```
<%
Dim defaultConnName
Function config_database(ByRef db)
	db.add "default", "Driver={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath(SCRIPTDIR) & "\sql\access\peetan.mdb", DB_TYPE_ACCESS
	db.add "mysql", "dsn=peetan;uid=peetan;pwd=peetan;", DB_TYPE_MYSQL
	db.add "postgresql", "dsn=peetan_pg;uid=peetan;pwd=peetan;", DB_TYPE_POSTGRESQL
	db.add "sqlserver", "dsn=peetan_sql;uid=peetan;pwd=peetan;", DB_TYPE_SQLSERVER
End Function
Function config_models(ByRef c)
	c.AddModel "models/blog_data.asp", "Blog_data", null, null, null
End Function
Function config_components(ByRef c)

End Function
Function config_controller(ByRef c)
	c.template = "template.html"
End Function
%>
```

blog_data.asp

```
Class Blog_data
	Public parent
	Public Sub Class_Initialize()
		Set Me.parent = New Model
		With Me.parent
			.SetChild Me
			.table = "blog_data"
			.validatefunc.Add "title", "notempty"
			.validatefunc.Add "body", "notempty"
			.validatemsg.Add "title", "Please input title.<br>"
			.validatemsg.Add "body", "Please input body.<br>"
		End With
	End Sub
End Class
```

template.html

```
<!--#include file="../peetan/render.asp"-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
<title>Peetan blog</title>
</head>
<body>
<%=view.content%>
</body>
</html>
```

#Create（生成）
add.asp

```
<!--#include file="./config.asp"-->
<!--#include file="../peetan/peetan.asp"-->
<%
Function action(ByRef c)
	errmsg = ""
	If c.post.Count > 0 Then
		errmsg = c.m("Blog_data").validatem(c.data("blog"))
		If errmsg = "" Then
			If IsObject(c.m("Blog_data").insert(c.data("blog"))) Then
				c.redirect("view.asp")
			End If
			errmsg = "データ登録失敗"
		End If
		Set data = c.data("blog")
	Else
		Set data = CreateDictionary()
	End If
	c.setv "errmsg", errmsg
	c.setv "data", data
End Function
%>
```

add.html

```
<!--#include file="../peetan/render.asp"-->
<form method="post" action="add.asp">
<font color="red"><%=data("errmsg")%></font>
TITLE<br>
<input type="text" name="blog/title" value="<%=s.html(data("data")("title"))%>"><br>
BODY<br>
<textarea cols="40" rows="6" name="blog/body">
<%=s.html(data("data")("body"))%></textarea><br>
<input type="submit" value="WRITE">
</form>
<a href=".">back</a>
```

#Read（読み取り）
view.asp

```
<!--#include file="./config.asp"-->
<!--#include file="../peetan/peetan.asp"-->
<%
Function action(ByRef c)
	c.viewfile = "view.html"
	c.setv "datas", c.m("Blog_data").find("", "modified DESC", "", "")
End Function
Function after_action(ByRef c)

End Function
%>
```

view.html

```
<!--#include file="../peetan/render.asp"-->
<p>
<a href="add.asp">Write new</a>
</p>
<% For Each d in data("datas").Items %>
<table border=1 cellpadding=8 cellspacing=0>
	<tr>
		<td>
			<%=s.html(d("title"))%>&nbsp;
			<a href="edit.asp?id=<%=d("id")%>">Edit</a>&nbsp;
			<a href="del.asp?id=<%=d("id")%>">Del</a>
		</td>
	</tr>
	<tr>
		<td><%=Replace(s.html(d("body")), vbCrLf, "<br>")%></td>
	</tr>
	<tr>
		<td><%=d("modified")%></td>
	</tr>
</table>
<br>
<% Next %>
```

#Update（更新）

edit.asp

```
<!--#include file="./config.asp"-->
<!--#include file="../peetan/peetan.asp"-->
<%
Function action(ByRef c)
	errmsg = ""
	If c.post.Count > 0 Then
		errmsg = c.m("Blog_data").validatem(c.data("blog"))
		If errmsg = "" Then
			If IsObject(c.m("Blog_data").update(c.data("blog"))) Then
				c.redirect("view.asp")
			End If
			errmsg = "データ更新失敗"
		End If
		Set data = c.data("blog")
	Else
		Set data = c.m("Blog_data").findone("id=" & c.getd("id"), "")
	End If
	c.setv "errmsg", errmsg
	c.setv "data", data
End Function
%>
```

edit.html

```
<!--#include file="../peetan/render.asp"-->
<form method="post" action="edit.asp?id=<%=data("data")("id")%>">
<font color="red"><%=data("errmsg")%></font>
TITLE<br>
<input type="text" name="blog/title" value="<%=s.html(data("data")("title"))%>"><br>
BODY<br>
<textarea cols="40" rows="6" name="blog/body">
<%=s.html(data("data")("body"))%></textarea><br>
<input type="hidden" name="blog/id" value="<%=data("data")("id")%>">
<input type="submit" value="UPDATE">
</form>
<a href=".">back</a>
```

#Delete（削除）

del.asp

```
<!--#include file="./config.asp"-->
<!--#include file="../peetan/peetan.asp"-->
<%
Function action(ByRef c)
	errmsg = ""
	If c.post.Count > 0 Then
		errmsg = ""
		If errmsg = "" Then
			If IsObject(c.m("Blog_data").del("id=" & c.post("id"))) Then
				c.redirect("view.asp")
			End If
			errmsg = "データ削除失敗"
		End If
	End If
	c.setv "errmsg", errmsg
	c.setv "data", c.m("Blog_data").findone("id=" & c.getd("id"), "")
End Function
%>
```

del.html

```
<!--#include file="../peetan/render.asp"-->
<form method="post" action="del.asp">
Delete OK?<br>
TITLE<br>
<%=s.html(data("data")("title"))%><br>
BODY<br>
<%=Replace(s.html(data("data")("body")), vbCrLf, "<br>")%><br>
<input type="hidden" name="id" value="<%=data("data")("id")%>">
<input type="submit" value="DELETE"><br>
<a href=".">back</a>
</form>
```