public virtual IHttpActionResult Post([FromBody] TEntity request) { HttpResponseMessage response = new HttpResponseMessage(); try { if (!ModelState.IsValid) { response = Request.CreateResponse(HttpStatusCode.BadRequest, ModelState); response.Headers.Add("X-Server-Message", "Formato ou inexistência de dados necessários a inserção em nossa base de dados."); return(ResponseMessage(response)); } BaseService <TEntity> service = new BaseService <TEntity>(); var entidade = service.Add(request); response = Request.CreateResponse(HttpStatusCode.OK, entidade); response.Headers.Add("X-Server-Message", $"{typeof(TEntity).Name} Cadastrado com sucesso."); } catch (Exception ex) { response = Request.CreateResponse(HttpStatusCode.InternalServerError, ex); response = ExceptionFilter.Find(ref response, ex); } return(ResponseMessage(response)); }
public virtual IHttpActionResult Get(int id) { HttpResponseMessage response = new HttpResponseMessage(); try { BaseService <TEntity> service = new BaseService <TEntity>(); var entidade = service.Find(id); response = Request.CreateResponse(HttpStatusCode.OK, entidade); response.Headers.Add("X-Server-Message", $"{typeof(TEntity).Name} encontrado."); } catch (Exception ex) { response = Request.CreateResponse(HttpStatusCode.InternalServerError, ex); response = ExceptionFilter.Find(ref response, ex); } return(ResponseMessage(response)); }
public virtual IHttpActionResult Delete(int id) { HttpResponseMessage response = new HttpResponseMessage(); try { BaseService <TEntity> service = new BaseService <TEntity>(); bool wasDeleted = service.Delete(id); if (wasDeleted) { response = Request.CreateResponse(HttpStatusCode.NoContent); response.Headers.Add("X-Server-Message", $"{typeof(TEntity).Name} excluido com sucesso."); } } catch (Exception ex) { response = Request.CreateResponse(HttpStatusCode.InternalServerError, ex); response = ExceptionFilter.Find(ref response, ex); } return(ResponseMessage(response)); }