-
Notifications
You must be signed in to change notification settings - Fork 2
/
TransactionController.cs
136 lines (117 loc) · 4.52 KB
/
TransactionController.cs
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using DkpWeb.Data;
using DkpWeb.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Sakura.AspNetCore;
using System;
using System.Linq;
using System.Threading.Tasks;
namespace DkpWeb.Controllers
{
[Authorize(Roles = "DKP")]
public class TransactionController : Controller
{
//
// GET: /Transaction/
public TransactionController(ApplicationDbContext data)
{
this.mData = data;
}
private ApplicationDbContext mData;
public ActionResult Index(int? page)
{
var totalSize = mData.Transaction.Count();
var currentPage = page ?? 1;
var pageSize = TransactionList.PageSize;
if (currentPage < 1)
return this.NotFound();
var trans = mData.Transaction.OrderByDescending(t => t.Created).ToPagedList(pageSize, currentPage);
var personMap = mData.Person.ToDictionary(p => p.Id);
foreach (var t in trans)
{
t.SetPrettyDescription(personMap);
}
//convert back to 1-based indexing for view
var model = new TransactionList(currentPage + 1, totalSize, trans);
return View(model);
}
public ActionResult View(Guid id)
{
var trans = mData.Transaction.Include(t => t.Creditor).Include(t => t.Debtor).Where(t => t.Id == id).Single();
trans.SetPrettyDescription(mData.Person.ToDictionary(p => p.Id));
return View(trans);
}
public ActionResult TopScore()
{
var peopleMap = mData.Person.ToDictionary(p => p.Id);
var q = mData.Transaction
.Where(t => !t.Description.StartsWith(Transaction.DebtTransferString))
.GroupBy(t => t.Description)
.Select(g => new TopScoreEntry { Name = g.Key, Amount = g.Sum(t => t.Amount) })
.OrderByDescending(g => g.Amount)
.ToList();
foreach (var t in q)
{
t.Name = Transaction.CreatePrettyDescription(t.Name, t.Amount, peopleMap) ?? t.Name;
}
return View(q);
}
public ActionResult Add()
{
ViewBag.People = mData.ActivePeopleOrderedByName.ToList();
return View(new NewTransactionModel());
}
[HttpPost]
[Authorize(Roles = "Admin")]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Delete(Guid id)
{
var trans = mData.Transaction.Where(t => t.Id == id).Single();
mData.Transaction.Remove(trans);
await mData.SaveChangesAsync();
return RedirectToAction("Index");
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Add(NewTransactionModel model)
{
if (ModelState.IsValid)
{
Person debtor = null, creditor = null;
if (model.Creditor == model.Debtor)
{
ModelState.AddModelError("", "The debtor and creditor must not be the same person.");
}
else
{
debtor = mData.Person.Where(p => p.Id == model.Debtor).SingleOrDefault();
creditor = mData.Person.Where(p => p.Id == model.Creditor).SingleOrDefault();
if (debtor == null)
{
ModelState.AddModelError("Debtor", "Invalid debtor.");
}
if (creditor == null)
{
ModelState.AddModelError("Creditor", "Invalid creditor.");
}
}
if (creditor != null && debtor != null)
{
var trans = new Transaction();
trans.CreditorId = creditor.Id;
trans.DebtorId = debtor.Id;
trans.Description = model.Description;
trans.Amount = (int)Math.Round(model.Amount * 100d);
trans.Created = DateTime.UtcNow;
trans.Id = Guid.NewGuid();
mData.Transaction.Add(trans);
await mData.SaveChangesAsync();
return RedirectToAction("Index", "Transaction");
}
}
ViewBag.People = mData.ActivePeopleOrderedByName.ToList();
return View(model);
}
}
}