Outline

Solution: Creating Custom Models

In order to build out the Api Endpoints we'll need some Custom Models in place before we start.

In the Conduit.Models project, add the following models to the Responses namespace:

Add the Comment Class with the following properties
Visibility | Type         | Name
------------------------------------------------------------------
  public   | int          | Id
  public   | DateTime     | CreatedAt
  public   | DateTime     | UpdatedAt
  public   | string       | Body
  public   | UserProfile  | Author

Initialize the Author Property in the constructor.

Here's the code to the Comment.cs file:

using System;

namespace Conduit.Models.Responses
{
    public class Comment
    {
        public int Id { get; set; }
        public DateTime CreatedAt { get; set; }
        public DateTime UpdatedAt { get; set; }
        public string Body { get; set; }
        public UserProfile Author { get; set; }

        public Comment()
        {
            Author = new UserProfile();
        }
    }
}
Add the Article Class with the following properties
Visibility | Type         | Name
------------------------------------------------------------------
  public   | string       | Slug
  public   | string       | Title
  public   | string       | Description
  public   | string       | Body
  public   | string[]     | TagList
  public   | DateTime     | CreatedAt
  public   | DateTime     | UpdatedAt
  public   | bool         | Favorited
  public   | int          | FavoritesCount
  public   | UserProfile  | Author

Here's the code for the Article.cs file:

using System;

namespace Conduit.Models.Responses
{
    public class Article
    {
        public string Slug { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public string Body { get; set; }
        public string[] TagList { get; set; }
        public DateTime CreatedAt { get; set; }
        public DateTime UpdatedAt { get; set; }
        public bool Favorited { get; set; }
        public int FavoritesCount { get; set; }
        public UserProfile Author { get; set; }
    }
}

In the Conduit.Models project, add the following models to the Requests namespace:

Add the ArticleRequest<T> Class with the following properties
Visibility | Type         | Name
------------------------------------------------------------------
  public   | T            | Article

This is a Generic Class. We created a Generic for UserRequest.

Here's the code to the ArticleRequest.cs file:

namespace Conduit.Models.Requests
{
    public class ArticleRequest<T>
    {
        public T Article { get; set; }
    }
}
Add the ArticleCreateRequest Class with the following properties
Visibility | Type     | Name        | Rules                      | Error Message
---------------------------------------------------------------------------------------------------
  public   | string   | Title       | Required, StringLenth(50)  | Title max character length is 50.
  public   | string   | Description | Required, StringLenth(50)  | Title max character length is 50.
  public   | string   | Body        | Required, StringLenth(200) | Body max character length is 200.
  public   | string[] | TagList

Here's the code to the ArticleCreateRequest.cs file:

using System.ComponentModel.DataAnnotations;

namespace Conduit.Models.Requests
{
    public class ArticleCreateRequest
    {
        [Required]
        [StringLength(50, ErrorMessage = "Title max character length is 50.")]
        public string Title { get; set; }

        [Required]
        [StringLength(50, ErrorMessage = "Description max character length is 50.")]
        public string Description { get; set; }

        [Required]
        [StringLength(200, ErrorMessage = "Body max character length is 200.")]
        public string Body { get; set; }

        public string[] TagList { get; set; }
    }
}
Add the ArticleUpdateRequest Class with the following properties
Visibility | Type     | Name        | Rules                      | Error Message
---------------------------------------------------------------------------------------------------
  public   | string   | Title       | Required, StringLenth(50)  | Title max character length is 50.
  public   | string   | Description | Required, StringLenth(50)  | Title max character length is 50.
  public   | string   | Body        | Required, StringLenth(200) | Body max character length is 200.

Here's the code to the ArticleUpdateRequest.cs file:

using System.ComponentModel.DataAnnotations;

namespace Conduit.Models.Requests
{
    public class ArticleUpdateRequest
    {
        [Required]
        [StringLength(50, ErrorMessage = "Title max character length is 50.")]
        public string Title { get; set; }

        [Required]
        [StringLength(50, ErrorMessage = "Description max character length is 50.")]
        public string Description { get; set; }

        [Required]
        [StringLength(200, ErrorMessage = "Body max character length is 200.")]
        public string Body { get; set; }
    }
}
Add the CommentRequest<T> Class with the following properties
Visibility | Type         | Name
------------------------------------------------------------------
  public   | T            | Comment

Here's the code to the CommentRequest.cs file:

namespace Conduit.Models.Requests
{
    public class CommentRequest<T>
    {
        public T Comment { get; set; }
    }
}
Add the CommentAddRequest Class with the following properties
Visibility | Type     | Name        | Rules                      | Error Message
---------------------------------------------------------------------------------------------------
  public   | string   | Body        | Required, StringLenth(200) | Body max character length is 200.

Here's the code to the CommentAddRequest.cs file:

using System.ComponentModel.DataAnnotations;

namespace Conduit.Models.Requests
{
    public class CommentAddRequest
    {
        [Required]
        [StringLength(200, ErrorMessage = "Body max character length is 200.")]
        public string Body { get; set; }
    }
}
 

I finished! On to the next chapter