Friends,
It's been several years since I last worked with the ASP.NET Web API framework. Even then I had developed with it minimally.
I have a requirement for a service that will allow for the creation of a new question, retrieval of all questions, and then retrieving a question by an eventId. The following code is my controller, model factory, and repository logic.
If someone wouldn't mind taking a look at the code, and offering their suggestions, I would greatly appreciate it.
Controller Code
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using QuestionDomain.Repositories;
using QuestionDomain.Services.DTOs;
namespace QuestionDomain.Services.Controllers
{
public class QuestionController : ApiController
{
private QuestionRepository _repository;
private ModelFactory _modelFactory;
public QuestionController()
{
_repository = new QuestionRepository();
_modelFactory = new ModelFactory();
}
[HttpGet]
[Route("api/question")]
[ResponseType(typeof(QuestionDTO))]
public HttpResponseMessage Get()
{
try
{
var questions = _repository?.Get();
if (questions == null || !questions.Any())
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "No questions to return");
}
var questionDtos = questions.Select(question => _modelFactory.Create(question)).ToList();
return Request.CreateResponse(HttpStatusCode.OK, questionDtos);
}
catch (Exception ex)
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "An exception occurred." + ex.Message);
}
}
[HttpGet]
[Route("api/question/{eventId}")]
[ResponseType(typeof(QuestionDTO))]
public HttpResponseMessage Get(Guid eventId)
{
var _eventId = eventId;
try
{
var question = _repository?.Get(eventId);
if (question == null)
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "No question to return");
}
var questionDto = _modelFactory.Create(question);
return Request.CreateResponse(HttpStatusCode.OK, questionDto);
}
catch (Exception ex)
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "An exception occurred." + ex.Message);
}
}
[HttpPost]
[Route("api/question")]
public HttpResponseMessage Post([FromBody]QuestionDTO questionDto)
{
try
{
var _question = _modelFactory.Create(questionDto);
if (_question == null)
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Question is null");
var result = _repository.Add(_question);
return Request.CreateResponse(HttpStatusCode.OK, result);
}
catch (Exception ex)
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "An exception occurred." + ex.Message);
}
}
}
}
Model Factory Code
using System;
using QuestionDomain.Models.models;
namespace QuestionDomain.Services.DTOs
{
public class ModelFactory
{
public QuestionDTO Create(Question question)
{
var _question = question;
var _questionDto = new QuestionDTO()
{
QuestionId = _question.QuestionId,
AssetId = _question.AssetId,
CustomerId = _question.CustomerId,
DateCreated = _question.DateCreated,
DateModified = _question.DateModified,
EventId = _question.EventId,
QuestionAnswer = _question.QuestionAnswer,
QuestionTitle = _question.QuestionTitle,
TimePosition = _question.TimePosition,
UserFirstName = _question.UserFirstName,
UserLastName = _question.UserLastName,
UserEmail = _question.UserEmail,
UserId = _question.UserId
};
return _questionDto;
}
public Question Create(QuestionDTO questionDto)
{
var _questionDto = questionDto;
var question = new Question()
{
QuestionId = Guid.NewGuid(),
AssetId = _questionDto.AssetId,
CustomerId = _questionDto.CustomerId,
DateCreated = _questionDto.DateCreated,
DateModified = _questionDto.DateModified,
EventId = _questionDto.EventId,
QuestionAnswer = _questionDto.QuestionAnswer,
QuestionTitle = _questionDto.QuestionTitle,
TimePosition = _questionDto.TimePosition,
UserFirstName = _questionDto.UserFirstName,
UserLastName = _questionDto.UserLastName,
UserEmail = _questionDto.UserEmail,
UserId = _questionDto.UserId
};
return question;
}
}
}
Repository Code
using System;
using System.Collections.Generic;
using System.Linq;
using QuestionDomain.Models.models;
using QuestionDomain.DataModel;
using QuestionDomain.Repositories.Interfaces;
namespace QuestionDomain.Repositories
{
public class QuestionRepository : IQuestionRepository
{
public IEnumerable<Question> Get()
{
try
{
using (var context = new QuestionContext())
{
var questions = context.Questions.ToList();
return questions;
}
}
catch (NullReferenceException ex)
{
throw new NullReferenceException("A null exception occurred getting all the questions. " + ex.Message);
}
catch (Exception ex)
{
throw new Exception("An exception occurred getting all the questions. " + ex.Message);
}
}
public Question Get(Guid eventId)
{
var _eventId = eventId;
try
{
using (var context = new QuestionContext())
{
var question = context.Questions
.Where(x => x.EventId == _eventId)
.Select(x => x).FirstOrDefault();
return question;
}
}
catch (Exception ex)
{
throw new Exception("An exception occurred saving a new question. " + ex.Message);
}
}
public string Add(Question question)
{
var _question = question;
try
{
if (_question == null)
return "Question is null";
using (var context = new QuestionContext())
{
context.Questions.Add(question);
context.SaveChanges();
}
return "question saved";
}
catch (Exception ex)
{
throw new Exception("An exception occurred saving a new question. " + ex.Message);
}
}
}
}