Written on 4/18/2014 in Web development

First phase - The Vorpal Blog


The vorpal story Web First phase MVC ASP.NET MVC

<h2>Hello Mr. Reader</h2>

This will be the last post in the first series of development, for those just joining, start reading here. We'll be talking about the web layer for which ASP.Net MVC5 is used. It'll be a very short post since the web layer will only consist of a few controllers and some basic dependency injection. And I already covered dependency injection in a previous post. The basic controllers will obviously be: TopicController, TagController and PostController. I'll also have to think about SEO in the near future.

The post controller looks like this:

[RoutePrefix("Blog")]
[Route("{action=LastPost}")]
public class PostController : Controller
{
    private readonly IPostApiClient _postClient;

    public PostController(IPostApiClient postClient)
    {
        _postClient = postClient;
    }

    //
    // GET: /Post/
    [Route("Posts/", Name = "AllPostsRoute")]
    public async Task<ActionResult> All(int? page)
    {
        ViewBag.CurrentRoute = "AllPostsRoute";
        var pageBrick = new UrlBricks();
        pageBrick.Parameters.Add("page", page.HasValue ? page.Value : 1);
        var result = await _postClient.Get<ListResponse<PostShortDto>>(pageBrick);
        if (result.Success)
        {
            return View("List", result.Object);
        }

        return View("List");
    }

    //
    // GET: /Post/
    [Route("{topic}/Posts/", Name = "AllPostsByTopicRoute")]
    public async Task<ActionResult> AllByTopic(int? page, string topic)
    {
        ViewBag.CurrentRoute = "AllPostsByTopicRoute";
        var result = await _postClient.GetPagedPostsForTopic(topic, page.HasValue ? page.Value : 1);
        if (result.Success)
        {
            return View("List", result.Object);
        }

        return View("List");
    }

    //
    // GET: /Post/
    [Route("Posts/ByTag/{tag}", Name = "AllPostsByTagRoute")]
    public async Task<ActionResult> AllByTag(int? page, string tag)
    {
        ViewBag.CurrentRoute = "AllPostsByTagRoute";
        var result = await _postClient.GetPagedPostsForTag(tag, page.HasValue ? page.Value : 1);
        if (result.Success)
        {
            return View("List", result.Object);
        }

        return View("List");
    }

    [Route]
    [Route("~/", Name = "default")]
    [Route("Post/LastPost/{topic?}", Name = "LastPostRoute")]
    public async Task<ActionResult> LastPost(string topic)
    {
        var post = await _postClient.GetLastPost(topic);
        return RedirectToRoute("PostDetailsRoute", new { topic = post.Topic.Name, id = post.Id, title = post.Title });
    }

    [Route("{topic}/Post/{id:int}/{title?}", Name = "PostDetailsRoute")]
    public async Task<ActionResult> Details(int id, string title, string topic)
    {
        var bricks = new UrlBricks();
        bricks.Parameters.Add("id", id);
        bricks.Parameters.Add("topic", topic);
        var result = await _postClient.Get<PostDto>(bricks);
        if (result.Success)
        {
            return View(result.Object);
        }
        return View();
    }
}

All there is to do now, is convert my notepad++ made html pages to razor and the css pages to less. Ofcourse, I will need to handle some errors as well because right now, just returning 'View()' will result in status codes 5003 errors.

That's it 'as is'. A lot can be improved, a lot won't be improved in the near future. I will continue to update where needed. There was no bloody wedding in the back-end series, so for a good story, I can suggest multiple other books / series1. Perhaps I can use the term 'Kill your babies' in another way than it was meant, for your reading pleasure2. Anyhow, I hope to see you reading the next post. I heard somewhere that reading this blog every week results in 70 virgins when you go to heaven. Or this could have been about some other extremist religion.

About the 70 virgins, isn't it strange you'll be getting (only) 70? You can only use them as a 'virgin' once. So an eternity with 70 virgins would actually be sad if you're really doing it for the virgin part. Unless they 'refresh' and in that case, why do you need 70? But hey, that's just my twisted mind worrying about how illogical these things sound to me. They really should think these things through a bit more.

Newer Older Top
blog comments powered by Disqus