﻿using AppLogic.DTOs;
using AppLogic.Helpers;
using AppLogic.Models;
using DB;
using Logic.Models;
using System;
using System.Linq;
using System.Web.Http;

/// <summary>
/// Сводное описание для PurchasesController
/// </summary>
public class PurchasesController: ApiController
{
    [Authorize]
    public EntityResult get()
    {
        var db = new DBContext().Purchases.ToList();
        var data = MapHelper<Purchase, PurchaseDTO>.get(db);

        return new EntityResult
        {
            Success = true,
            Count = data.Count,
            data = data
        };
    }

    [Authorize]
    public EntityResult get(Guid id)
    {
        var db = new DBContext().Purchases.Where(a => a.Id == id).Select(a => a).ToList();
        var data = MapHelper<Purchase, PurchaseDTO>.get(db);

        return new EntityResult
        {
            Success = true,
            Count = data.Count,
            data = data
        };
    }

    [Authorize]
    public void put(Guid id, [FromBody]PurchaseDTO item)
    {

    }

    [Authorize]
    public void post([FromBody]PurchaseDTO item)
    {

    }

    [Authorize]
    public void delete(Guid id)
    {
        var db = new DBContext();
        var item = db.Purchases.Where(a => a.Id == id).SingleOrDefault();
        if (item != null)
        {
            db.Purchases.Remove(item);
            db.SaveChanges();
        }
    }
}