aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/TCC/Tango.TCC.Service/Controllers/CardsController.cs
blob: 221969b239c76782e70fef1dc5df2a20e8813557 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System;
using System.Collections.Generic;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Tango.TCC.BL.Entities;
using Tango.TCC.BL.Web;
using Tango.TCC.Service.DB;
using Tango.TCC.Service.DTO;
using Tango.TCC.Service.Filters;
using Tango.TCC.Service.Security;
using Tango.Web.Controllers;

namespace Tango.TCC.Service.Controllers
{
    [JwtTokenFilter]
    public class CardsController : TangoController<TokenObject>
    {
        [JwtTokenFilter]
        [HttpGet]
        public List<CardDTO> GetCards()
        {
            List<CardDTO> results = new List<CardDTO>();

            using (var db = TccDbContext.CreateTCC())
            {
                var dbResults = db.Cards.Select(x=>x).ToList();
                foreach (var item in dbResults)
                {
                    CardDTO dto = new CardDTO();
                    dto.CardCode = item.Code;
                    dto.Columns = item.Columns;
                    dto.Rows = item.Rows;
                    dto.TargetIndex = item.TargetIndex;
                    dto.Version = item.Version;
                    dto.Batch = item.Batch;
                    dto.IsHomePrinted = item.IsHomePrinted;
                    results.Add(dto);
                }
            }

            return results;
        }

        [JwtTokenFilter]
        [HttpPost, ActionName("Edit")]
        public void EditCard(int? id, [FromUri] ResultsByDateRequest request)
        {
            using (var db = TccDbContext.CreateTCC())
            {
                if (id == null)
                {
                    var dbNewCard = new Card();
                  // dbNewCard.Code = request.Code;
                  //  dbNewCard.Columns = request.Columns;
                  //  dbNewCard.TargetIndex = request.TargetIndex;
                    dbNewCard.IsHomePrinted = false;
                    db.Cards.Add(dbNewCard);
                    
                }
                else
                {
                    var dbEditCard = db.Cards.First(i => i.ID == id);
                    if (dbEditCard == null)
                    {
                        return;// HttpNotFoundResult();
                    }
                }

                try
                {
                    db.SaveChanges();
                    
                }
                catch (RetryLimitExceededException /* dex */)
                {
                    ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
                }
            }
        }
    }
}