<commit_before>﻿using ApiRest.ViewModels;
using AutoMapper;
using Domain.Interfaces;
using Domain.Objetos;
using Domain.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;

namespace ApiRest.Controllers
{
    
    [RoutePrefix("api/pedidos")]
    public class PedidosController : ApiController
    {
        private IUnitOfWork _uow;

        public PedidosController(IUnitOfWork uow)
        {
            _uow = uow;
        }

        [HttpGet]
        [Route("")]
        [Authorize(Roles = "Admin, Ponto")]
        public HttpResponseMessage GetAllPedidos()
        {
            var pedidos = new List<object>();
            var userId = User.Identity.Name;
            foreach (var pedido in _uow.PedidoRepository.ObterTodosPedidosDoUsuario(userId))
                pedidos.Add(Mapper.Map<Pedido, PedidoViewModelEnvio>(pedido));

            var response = Request.CreateResponse(HttpStatusCode.Accepted, pedidos);

            return response;
        }



        [HttpGet]
        [Route("{id}")]
        [Authorize(Roles = "Admin, Ponto")]
        public HttpResponseMessage GetByIdPedido([FromUri]int id)
        {
            var userId = User.Identity.Name;
            Pedido pedido = ServiceValidation.Exists(id, userId, _uow.PedidoRepository);
            if (pedido == null)
                return Request.CreateResponse(HttpStatusCode.NotFound);

            var pedidoView = Mapper.Map<Pedido, PedidoViewModelEnvio>(pedido);
            var response = Request.CreateResponse(HttpStatusCode.Accepted, pedidoView);

            return response;
        }

        [HttpGet]
        [Route("{id}/itens")]
        public HttpResponseMessage GetItensDoPedido([FromUri]int id)
        {
            var itens = new List<object>();
            var userId = User.Identity.Name;
            Pedido pedido = ServiceValidation.Exists(id, userId, _uow.PedidoRepository);
            if (pedido == null)
                return Request.CreateResponse(HttpStatusCode.NotFound);

            foreach (var item in pedido.ItensDoPedido)
                itens.Add(Mapper.Map<ItemDoPedido, ItemDoPedidoViewModelEnvio>(item));

            var response = Request.CreateResponse(HttpStatusCode.Accepted, itens);

            return response;
        }

        [HttpGet]
        [Route("{idPedido}/itens/id")]
        public HttpResponseMessage GetByIdItemDoPedido([FromUri] int idPedido, [FromUri]int id)
        {
            var userId = User.Identity.Name;
            Pedido pedido = ServiceValidation.Exists(idPedido, userId, _uow.PedidoRepository);
            if (pedido == null)
                return Request.CreateResponse(HttpStatusCode.NotFound);

            var item = pedido.ItensDoPedido.Find(e => e.Id == id);
            var itemView = Mapper.Map<ItemDoPedido, ItemDoPedidoViewModelEnvio>(item);

            var response = Request.CreateResponse(HttpStatusCode.Accepted, itemView);

            return response;
        }

        [HttpPost]
        [Route("")]
        public HttpResponseMessage PostProduto([FromBody] PedidoViewModelRecebimento pedidoViewModel)
        {
            if (!ModelState.IsValid)
            {
                var errors = ModelState.Select(m => string.Join(",", m.Value.Errors.Select(e => e.ErrorMessage)));
                return Request.CreateResponse(HttpStatusCode.BadRequest, new { Errors = errors });
            }
            var pedido = Mapper.Map<PedidoViewModelRecebimento, Pedido>(pedidoViewModel);

            var userId = User.Identity.Name;
            PontoDeVenda ponto = ServiceValidation.Exists(pedido.PontoDeVenda.Id, userId, _uow.PontoDeVendaRepository);
            if (ponto == null)
                return Request.CreateResponse(HttpStatusCode.BadRequest, "Usuário do ponto de venda invalido.");

            try
            {
                var servicePedido = new ServicePedido(_uow);

                servicePedido.CompletarPedido(pedido);
                var response = Request.CreateResponse(HttpStatusCode.Created);
				response.Headers.Location = new Uri(HttpContext.Current.Request.Url.AbsoluteUri + '/' + pedido.Id);
                return response;

            }<commit_msg>commit de ajustes de bugs dos controllers<commit_after>﻿using ApiRest.ViewModels;
using AutoMapper;
using Domain.Interfaces;
using Domain.Objetos;
using Domain.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;

namespace ApiRest.Controllers
{
    
    [RoutePrefix("api/pedidos")]
    public class PedidosController : ApiController
    {
        private IUnitOfWork _uow;

        public PedidosController(IUnitOfWork uow)
        {
            _uow = uow;
        }

        [HttpGet]
        [Route("")]
        [Authorize(Roles = "Admin, Ponto")]
        public HttpResponseMessage GetAllPedidos()
        {
            var pedidos = new List<object>();
            var userId = User.Identity.Name;
            foreach (var pedido in _uow.PedidoRepository.ObterTodosPedidosDoUsuario(userId))
                pedidos.Add(Mapper.Map<Pedido, PedidoViewModelEnvio>(pedido));

            var response = Request.CreateResponse(HttpStatusCode.Accepted, pedidos);

            return response;
        }



        [HttpGet]
        [Route("{id}")]
        [Authorize(Roles = "Admin, Ponto")]
        public HttpResponseMessage GetByIdPedido([FromUri]int id)
        {
            var userId = User.Identity.Name;
            Pedido pedido = ServiceValidation.Exists(id, userId, _uow.PedidoRepository);
            if (pedido == null)
                return Request.CreateResponse(HttpStatusCode.NotFound);

            var pedidoView = Mapper.Map<Pedido, PedidoViewModelEnvio>(pedido);
            var response = Request.CreateResponse(HttpStatusCode.Accepted, pedidoView);

            return response;
        }

        [HttpGet]
        [Route("{id}/itens")]
        [Authorize(Roles = "Admin, Ponto")]
        public HttpResponseMessage GetItensDoPedido([FromUri]int id)
        {
            var itens = new List<object>();
            var userId = User.Identity.Name;
            Pedido pedido = ServiceValidation.Exists(id, userId, _uow.PedidoRepository);
            if (pedido == null)
                return Request.CreateResponse(HttpStatusCode.NotFound);

            foreach (var item in pedido.ItensDoPedido)
                itens.Add(Mapper.Map<ItemDoPedido, ItemDoPedidoViewModelEnvio>(item));

            var response = Request.CreateResponse(HttpStatusCode.Accepted, itens);

            return response;
        }

        [HttpGet]
        [Route("{idPedido}/itens/{id}")]
        [Authorize(Roles = "Admin, Ponto")]
        public HttpResponseMessage GetByIdItemDoPedido([FromUri] int idPedido, [FromUri]int id)
        {
            var userId = User.Identity.Name;
            Pedido pedido = ServiceValidation.Exists(idPedido, userId, _uow.PedidoRepository);
            if (pedido == null)
                return Request.CreateResponse(HttpStatusCode.NotFound);

            var item = pedido.ItensDoPedido.Find(e => e.Id == id);
            var itemView = Mapper.Map<ItemDoPedido, ItemDoPedidoViewModelEnvio>(item);

            var response = Request.CreateResponse(HttpStatusCode.Accepted, itemView);

            return response;
        }

        [HttpPost]
        [Route("")]
        [Authorize(Roles = "Admin, Ponto")]
        public HttpResponseMessage PostPedido([FromBody] PedidoViewModelRecebimento pedidoViewModel)
        {
            if (!ModelState.IsValid)
            {
                var errors = ModelState.Select(m => string.Join(",", m.Value.Errors.Select(e => e.ErrorMessage)));
                return Request.CreateResponse(HttpStatusCode.BadRequest, new { Errors = errors });
            }
            var pedido = Mapper.Map<PedidoViewModelRecebimento, Pedido>(pedidoViewModel);

            var userId = User.Identity.Name;
            PontoDeVenda ponto = ServiceValidation.Exists(pedido.PontoDeVenda.Id, userId, _uow.PontoDeVendaRepository);
            if (ponto == null)
                return Request.CreateResponse(HttpStatusCode.BadRequest, "Usuário do ponto de venda invalido.");

            try
            {
                var servicePedido = new ServicePedido(_uow);

                servicePedido.CompletarPedido(pedido);
                var response = Request.CreateResponse(HttpStatusCode.Created);
				response.Headers.Location = new Uri(HttpContext.Current.Request.Url.AbsoluteUri + '/' + pedido.Id);
                return response;

            }
{'fraction_non_alphanumeric': 0.09819477980912958, 'fraction_numerical': 0.0, 'mean_word_length': 2.333844384821771, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2752032', 'n_tokens_mistral': 2486, 'n_tokens_neox': 2386, 'n_words': 475}