forked from wojilu/wojilu
/
MainController.cs
executable file
·207 lines (145 loc) · 6.69 KB
/
MainController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
* Copyright (c) 2010, www.wojilu.com. All rights reserved.
*/
using System;
using System.Collections.Generic;
using wojilu.Web.Mvc;
using wojilu.Apps.Forum.Service;
using wojilu.Apps.Forum.Domain;
using wojilu.Apps.Forum.Interface;
using wojilu.Members.Groups.Domain;
using wojilu.Members.Groups.Service;
using wojilu.Members.Groups.Interface;
using wojilu.Members.Users.Domain;
using wojilu.Members.Users.Interface;
using wojilu.Members.Users.Service;
using wojilu.Web.Controller.Common;
using wojilu.Web.Mvc.Attr;
using wojilu.Web.Controller.Groups.Caching;
using wojilu.Members.Groups;
namespace wojilu.Web.Controller.Groups {
public class MainController : ControllerBase {
private static readonly ILog logger = LogManager.GetLogger( typeof( MainController ) );
public virtual IGroupService groupService { get; set; }
public virtual IGroupPostService postService { get; set; }
public virtual IUserService userService { get; set; }
public MainController() {
groupService = new GroupService();
postService = new GroupPostService();
userService = new UserService();
}
[CacheAction( typeof( GroupMainLayoutCache ) )]
public override void Layout() {
// 当前app/module所有页面,所属的首页
ctx.SetItem( "_moduleUrl", to( Index ) );
List<GroupCategory> categories = GroupCategory.GetAll();
IBlock block = getBlock( "categories" );
foreach (GroupCategory c in categories) {
block.Set( "c.Name", c.Name );
block.Set( "c.Link", to( List, c.Id ) );
block.Next();
}
String name = ctx.Get( "name" );
set( "s.Name", name );
set( "SearchAction", to( Search ) );
set( "allGroupLink", to( List, -1 ) );
bindAdminLink();
}
private void bindAdminLink() {
set( "groupAdminHome", to( new wojilu.Web.Controller.Admin.Groups.GroupController().Index ) );
set( "postLink", to( new wojilu.Web.Controller.Admin.Groups.GroupController().PostAdmin ) );
set( "groupLink", to( new wojilu.Web.Controller.Admin.Groups.GroupController().GroupAdmin, -1 ) );
set( "groupCategoryLink", to( new wojilu.Web.Controller.Admin.Groups.CategoryController().List ) );
set( "settingLink", to( new wojilu.Web.Controller.Admin.Groups.SettingController().Index ) );
}
[CachePage( typeof( GroupMainPageCache ) )]
[CacheAction( typeof( GroupMainActionCache ) )]
public virtual void Index() {
ctx.Page.Title = GroupSetting.Instance.MetaTitle;
ctx.Page.Keywords = GroupSetting.Instance.MetaKeywords;
ctx.Page.Description = GroupSetting.Instance.MetaDescription;
List<ForumTopic> posts = postService.GetHotTopic( 10 );
bindPosts( posts, "list" );
List<Group> hots = groupService.GetHots( 8 );
bindGroups( hots, "hots" );
List<Group> recent = groupService.GetRecent( 12 );
bindGroups( recent, "recent" );
set( "allGroupLink", to( List, -1 ) );
}
public virtual void List( long id ) {
ctx.Page.Title = "群组列表";
GroupCategory category = db.findById<GroupCategory>( id );
if (category != null) {
String groupsByCategory = string.Format( lang( "groupsByCategory" ), category.Name );
set( "groupsByCategory", groupsByCategory );
}
else {
set( "groupsByCategory", lang( "all" ) );
}
DataPage<Group> groups = groupService.GetByCategory( id );
bindGroups( groups.Results, "list" );
set( "page", groups.PageBar );
}
public virtual void Search() {
DataPage<Group> list = getResults();
bindGroups( list.Results, "list" );
set( "page", list.PageBar );
}
private DataPage<Group> getResults() {
String condition = "";
String name = ctx.Get( "name" );
if (strUtil.IsNullOrEmpty( name )) return DataPage<Group>.GetEmpty();
name = strUtil.SqlClean( name, 20 );
String queryType = ctx.Get( "queryType" );
if ("name".Equals( queryType )) {
condition += string.Format( " and Name like '%{0}%' ", name );
}
else if ("creator".Equals( queryType )) {
String creatorIds = getCreators( name );
if (strUtil.IsNullOrEmpty( creatorIds )) return DataPage<Group>.GetEmpty();
condition += " and CreatorId in (" + creatorIds + ")";
}
else if ("description".Equals( queryType )) {
condition += string.Format( " and Description like '%{0}%' ", name );
}
condition = condition.Trim();
condition = strUtil.TrimStart( condition, "and" );
return groupService.SearchByCondition( condition );
}
//----------------------------------------------------------------------------
private void bindPosts( List<ForumTopic> posts, String blockName ) {
IBlock block = getBlock( blockName );
foreach (ForumTopic post in posts) {
if (post.Creator == null) continue;
block.Set( "post.Title", post.Title );
block.Set( "post.Link", alink.ToAppData( post ) );
block.Set( "post.Replies", post.Replies );
Group g = getGroup( post );
if (g == null) continue;
block.Set( "post.OwnerName", g.Name );
block.Set( "post.OwnerLink", Link.ToMember( g ) );
block.Next();
}
}
private void bindGroups( List<Group> groups, String blockName ) {
IBlock gblock = getBlock( blockName );
foreach (Group g in groups) {
gblock.Set( "g.Name", g.Name );
gblock.Set( "g.Link", Link.ToMember( g ) );
gblock.Set( "g.Logo", g.LogoSmall );
gblock.Set( "g.Created", cvt.ToTimeString( g.Created ) );
gblock.Set( "g.MemberCount", g.MemberCount );
gblock.Next();
}
}
private Group getGroup( IPost post ) {
return groupService.GetById( post.OwnerId );
}
private String getCreators( String name ) {
List<User> users = userService.SearchByName( name );
String ids = "";
foreach (User user in users) ids += user.Id + ",";
return ids.TrimEnd( ',' );
}
}
}