Reverted accidental checkin
[castle.git] / Samples / MindDump / Castle.Applications.MindDump / Services / BlogService.cs
blob813173857e75bf796cb1102ca34de817d2f30384
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.Applications.MindDump.Services
17 using System;
18 using System.Collections;
20 using Castle.Applications.MindDump.Dao;
21 using Castle.Applications.MindDump.Model;
24 public class BlogService
26 private PostDao _postDao;
27 private BlogDao _blogDao;
28 private AuthorDao _authorDao;
29 private int _latestBlogsCount = 10;
30 private int _latestPostCount = 10;
32 public BlogService(AuthorDao authorDao, BlogDao blogDao, PostDao postDao)
34 _authorDao = authorDao;
35 _blogDao = blogDao;
36 _postDao = postDao;
39 /// <summary>
40 /// This value can be optionally adjusted using
41 /// the configuration associated with the kernel
42 /// </summary>
43 public int LatestBlogsCount
45 get { return _latestBlogsCount; }
46 set { _latestBlogsCount = value; }
49 /// <summary>
50 /// This value can be optionally adjusted using
51 /// the configuration associated with the kernel
52 /// </summary>
53 public int LatestPostCount
55 get { return _latestPostCount; }
56 set { _latestPostCount = value; }
59 public Post CreateNewPost(Blog blog, Post post)
61 try
63 post.Blog = blog;
64 return _postDao.Create(post);
66 catch(Exception ex)
68 throw new ApplicationException("Could not create post", ex);
72 public void UpdatePost(Blog blog, long postId, Post post)
74 try
76 Post originalPost = ObtainPost(blog, postId);
78 originalPost.Title = post.Title;
79 originalPost.Contents = post.Contents;
81 _postDao.Update(originalPost);
83 catch(Exception ex)
85 throw new ApplicationException("Could not update post", ex);
89 public Post ObtainPost( Blog blog, long postId )
91 try
93 Post post = _postDao.Find(postId);
95 if (post != null)
97 if (post.Blog.Id != blog.Id)
99 throw new ApplicationException("The post requested belongs " +
100 "to a different blog");
104 return post;
106 catch(ApplicationException ex)
108 throw ex;
110 catch(Exception ex)
112 throw new ApplicationException("Could not find post", ex);
116 public IList ObtainPosts(Blog blog)
118 return _postDao.Find(blog);
121 public Blog ObtainBlogByAuthorName(string authorName)
123 Author author = _authorDao.Find(authorName);
125 if (author == null)
127 return null;
130 return author.Blogs[0] as Blog;
133 public IList ObtainLatestBlogs()
135 return _blogDao.FindLatest(10);
138 public IList ObtainLatestPosts()
140 return _postDao.FindLatest(10);