Fixing an issue with output parameters that are of type IntPtr
[castle.git] / Samples / MindDump / Castle.Applications.MindDump / Services / BlogMaintenanceService.cs
blob0a09dfed76742ff80432beb86e87beb156a17cd7
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 BlogMaintenanceService
26 private PostDao _postDao;
27 private BlogDao _blogDao;
28 private AuthorDao _authorDao;
30 public BlogMaintenanceService(AuthorDao authorDao, BlogDao blogDao, PostDao postDao)
32 _authorDao = authorDao;
33 _blogDao = blogDao;
34 _postDao = postDao;
37 public Post CreateNewPost(Blog blog, Post post)
39 post.Blog = blog;
41 try
43 return _postDao.Create(post);
45 catch(Exception ex)
47 throw new ApplicationException("Could not create post", ex);
51 public void UpdatePost(Blog blog, long postId, Post post)
53 Post originalPost = ObtainPost(blog, postId);
55 originalPost.Title = post.Title;
56 originalPost.Contents = post.Contents;
58 try
60 _postDao.Update(originalPost);
62 catch(Exception ex)
64 throw new ApplicationException("Could not update post", ex);
68 public Post ObtainPost( Blog blog, long postId )
70 try
72 Post post = _postDao.Find(postId);
74 if (post != null)
76 if (post.Blog.Id != blog.Id)
78 throw new ApplicationException("The post requested belongs " +
79 "to a different blog");
83 return post;
85 catch(ApplicationException ex)
87 throw ex;
89 catch(Exception ex)
91 throw new ApplicationException("Could not find post", ex);
95 public IList ObtainPosts(Blog blog)
97 return _postDao.Find(blog);
100 public Blog ObtainBlogByAuthorName(string authorName)
102 Author author = _authorDao.Find(authorName);
104 if (author == null)
106 return null;
109 return author.Blogs[0] as Blog;