| 1 | using System;
|
|---|
| 2 | using System.Collections.Generic;
|
|---|
| 3 | using System.Linq;
|
|---|
| 4 |
|
|---|
| 5 | namespace Wierszowki.Core.Linq
|
|---|
| 6 | {
|
|---|
| 7 | public partial class WierszowkiDataContext
|
|---|
| 8 | {
|
|---|
| 9 | public Author GetAuthorById(int id)
|
|---|
| 10 | {
|
|---|
| 11 | return Authors.Single(a => a.Id == id);
|
|---|
| 12 | }
|
|---|
| 13 |
|
|---|
| 14 | public List<Author> GetAuthors()
|
|---|
| 15 | {
|
|---|
| 16 | return Authors.ToList();
|
|---|
| 17 | }
|
|---|
| 18 |
|
|---|
| 19 | public List<User> GetUsers()
|
|---|
| 20 | {
|
|---|
| 21 | this.GetTable<User>();
|
|---|
| 22 |
|
|---|
| 23 | return Users.ToList();
|
|---|
| 24 | }
|
|---|
| 25 |
|
|---|
| 26 | public User GetUserById(int id)
|
|---|
| 27 | {
|
|---|
| 28 | return Users.Single(u => u.Id == id);
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| 31 | public User GetUserByLogin(string login)
|
|---|
| 32 | {
|
|---|
| 33 | return Users.Single(u => u.Login == login);
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | public bool LoginUser(string login, string password)
|
|---|
| 37 | {
|
|---|
| 38 | var query = from u in Users
|
|---|
| 39 | where u.Login == login && u.Password == password
|
|---|
| 40 | select u;
|
|---|
| 41 |
|
|---|
| 42 | return query.Count() == 1;
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | public List<Magazine> GetMagazines()
|
|---|
| 46 | {
|
|---|
| 47 | return Magazines.ToList();
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | public List<EmploymentType> GetEmploymentTypes()
|
|---|
| 51 | {
|
|---|
| 52 | return EmploymentTypes.ToList();
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | public List<ItemType> GetItemTypes()
|
|---|
| 56 | {
|
|---|
| 57 | return ItemTypes.ToList();
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | public IQueryable<Issue> FindIssuesByMagazineId(int magazineId)
|
|---|
| 61 | {
|
|---|
| 62 | var startDate = DateTime.Now.AddMonths(-3);
|
|---|
| 63 | var endDate = DateTime.Now.AddMonths(1);
|
|---|
| 64 |
|
|---|
| 65 | var query = from i in Issues
|
|---|
| 66 | where i.MagazineId == magazineId
|
|---|
| 67 | && i.Date >= startDate
|
|---|
| 68 | && i.Date <= endDate
|
|---|
| 69 | select i;
|
|---|
| 70 | return query;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | public IQueryable<Issue> FindIssuesByMagazineItemId(int issueId)
|
|---|
| 74 | {
|
|---|
| 75 | var issue = Issues.Single(i => i.Id == issueId);
|
|---|
| 76 | return FindIssuesByMagazineId(issue.MagazineId);
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | public List<Report> FindItemsByYearMonthAndEmploymentType(int year, int month, int employmentType)
|
|---|
| 80 | {
|
|---|
| 81 | var query = from magazineItem in MagazineItems
|
|---|
| 82 | where magazineItem.Date.Month == month
|
|---|
| 83 | && magazineItem.Date.Year == year
|
|---|
| 84 | group magazineItem by new { magazineItem.Author, magazineItem.Issue.MagazineId}
|
|---|
| 85 | into m
|
|---|
| 86 | select new Report { FirstName = m.Key.Author.FirstName, LastName = m.Key.Author.LastName, Price = m.Sum(magazineItem => magazineItem.Price), Bonus = m.Sum(magazineItem => magazineItem.Bonus), Magazine = m.Key.MagazineId.ToString() };
|
|---|
| 87 |
|
|---|
| 88 | return query.ToList();
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | public List<MagazineItem> FindItemsByYearMonthAndAuthor(int year, int month, int author)
|
|---|
| 92 | {
|
|---|
| 93 | var query = from magazineItem in MagazineItems
|
|---|
| 94 | where magazineItem.Date.Month == month
|
|---|
| 95 | && magazineItem.Date.Year == year
|
|---|
| 96 | && magazineItem.AuthorId == author
|
|---|
| 97 | orderby magazineItem.Issue.MagazineId descending
|
|---|
| 98 | select magazineItem;
|
|---|
| 99 | return query.ToList();
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | public List<Report> GetAuthorsReport(int year, int month)
|
|---|
| 103 | {
|
|---|
| 104 | var authors = GetAuthors(year, month);
|
|---|
| 105 |
|
|---|
| 106 | var reports = new List<Report>();
|
|---|
| 107 | foreach (var author in authors)
|
|---|
| 108 | {
|
|---|
| 109 | var report = new Report
|
|---|
| 110 | {
|
|---|
| 111 | FirstName = author.FirstName,
|
|---|
| 112 | LastName = author.LastName,
|
|---|
| 113 | Price = author.Price.Value,
|
|---|
| 114 | Bonus = author.Bonus.Value
|
|---|
| 115 | };
|
|---|
| 116 | reports.Add(report);
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | return reports;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | public Dictionary<int, Dictionary<string, decimal>> GetAuthorsByMagzinesReport(int year, int month, List<Author> authors, List<Magazine> magazines)
|
|---|
| 123 | {
|
|---|
| 124 | var result = new Dictionary<int, Dictionary<string, decimal>>();
|
|---|
| 125 |
|
|---|
| 126 | foreach (var author in authors)
|
|---|
| 127 | {
|
|---|
| 128 | foreach (var magazine in magazines)
|
|---|
| 129 | {
|
|---|
| 130 | if (!result.ContainsKey(author.Id))
|
|---|
| 131 | result.Add(author.Id, new Dictionary<string, decimal>());
|
|---|
| 132 |
|
|---|
| 133 | if (!result[author.Id].ContainsKey(magazine.NickName))
|
|---|
| 134 | result[author.Id].Add(magazine.NickName, decimal.Zero);
|
|---|
| 135 | }
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | var authorsByMagazines = GetAuthorsByMagzines(year, month);
|
|---|
| 139 | foreach (var authorsByMagazine in authorsByMagazines)
|
|---|
| 140 | {
|
|---|
| 141 | if (result.ContainsKey(authorsByMagazine.Id))
|
|---|
| 142 | result[authorsByMagazine.Id][authorsByMagazine.NickName] = authorsByMagazine.Price.Value +
|
|---|
| 143 | authorsByMagazine.Bonus.Value;
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | return result;
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | public void AddUser(User user)
|
|---|
| 150 | {
|
|---|
| 151 | InsertUser(user);
|
|---|
| 152 | }
|
|---|
| 153 | }
|
|---|
| 154 | } |
|---|