Advertisement
Guest User

Untitled

a guest
Apr 1st, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.63 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.Common;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using Dapper;
  7. using DM.Entities;
  8. using Security;
  9.  
  10. namespace DM.Context
  11. {
  12.     public class AccountRepository:BaseRepository<Account>
  13.     {
  14.         public AccountRepository(DbConnection connection) : base(connection)
  15.         {
  16.         }
  17.  
  18.         #region New_methods
  19.         public Account GetBySessionKey(string sessionKey)
  20.         {
  21.             string sql = @"SELECT *
  22.                            FROM Accounts INNER JOIN Sessions ON Accounts.Id=Sessions.AccountId
  23.                            WHERE Sessions.SessionKey=@SessionKey";
  24.  
  25.             return DapperConnection.Query<Account>(sql, new { SessionKey = sessionKey }).FirstOrDefault();
  26.         }
  27.  
  28.         public async Task<Account> GetClientByEmail(string username)
  29.         {
  30.             var account = (await DapperConnection.GetListAsync<Account>("WHERE Username = @Username", new { Username = username })).FirstOrDefault();
  31.  
  32.             return account;
  33.         }
  34.         #endregion
  35.  
  36.         public async Task<IEnumerable<Account>> GetWatchedVideoAccounts()
  37.         {
  38.             string sql = @"SELECT * FROM Accounts INNER JOIN AccountCourses ON AccountCourses.AccountId=Accounts.Id
  39.                            WHERE AccountCourses.IsWatchedToday=1 AND AccountCourses.Day<60 AND Role=0";
  40.  
  41.  
  42.             return await DapperConnection.QueryAsync<Account, AccountCourse, Account>(sql, ((account, course) =>
  43.                 {
  44.                     account.CurrentAccountCourse = course;
  45.                     return account;
  46.                 }));
  47.         }
  48.  
  49.         public async Task<IEnumerable<Account>> GetUnsubscribedAccounts(long date)
  50.         {
  51.             string sql = @"SELECT * FROM Accounts WHERE Subscription<@Date AND Role=0";
  52.             return await DapperConnection.QueryAsync<Account>(sql, new { Date = date });
  53.         }
  54.  
  55.         public async Task<IEnumerable<Account>> GetActiveNonWatchedVideoAccounts(long date)
  56.         {
  57.             string sql = @"SELECT * FROM Accounts INNER JOIN AccountCourses ON AccountCourses.AccountId=Accounts.Id
  58.                            WHERE Accounts.Subscription>@Date AND AccountCourses.IsWatchedToday<>1 AND AccountCourses.Day<60 AND Role=0";
  59.  
  60.             return await DapperConnection.QueryAsync<Account>(sql, new { Date = date });
  61.         }
  62.  
  63.         public async Task<IEnumerable<Account>> GetClientsAsync(int? page)
  64.         {
  65.             if (page == null)
  66.                 return await DapperConnection.GetListAsync<Account>();
  67.             return await DapperConnection.GetListPagedAsync<Account>(page.Value, 20, null, null);
  68.         }
  69.  
  70.         public IEnumerable<Account> GetClients(int? page)
  71.         {
  72.             if (page == null)
  73.                 return DapperConnection.GetList<Account>();
  74.             return DapperConnection.GetListPaged<Account>(page.Value, 20, null, null);
  75.         }
  76.  
  77.         public async Task<Account> SelectAccount(string username, string password)
  78.         {
  79.             var  account = (await DapperConnection.GetListAsync<Account>("WHERE Username = @Username", new {Username=username})).FirstOrDefault();
  80.             if (account != null)
  81.             {
  82.                 string decryptedPassword;
  83.                 try
  84.                 {
  85.                     decryptedPassword = StringCipher.Decrypt(account.Password);
  86.                 }
  87.                 catch (Exception exception)
  88.                 {
  89.                     return null;
  90.                 }
  91.  
  92.                 if (String.Equals(decryptedPassword, password))
  93.                     return account;
  94.             }
  95.             return null;
  96.         }
  97.  
  98.         public override async Task<IEnumerable<Account>> GetList(int? page)
  99.         {
  100.             if (page == null)
  101.                 return await DapperConnection.GetListAsync<Account>();
  102.             return await DapperConnection.GetListPagedAsync<Account>(page.Value, 20, null, null);
  103.         }
  104.  
  105.         public override async Task<Account> GetById(int id)
  106.         {
  107.             return await DapperConnection.GetAsync<Account>(id);
  108.         }
  109.  
  110.         public override  Account Save(Account entity)
  111.         {
  112.             if (entity.Id == 0)
  113.             {
  114.                 var id = DapperConnection.Insert(entity);
  115.                 if (id != null) entity.Id = id.Value;
  116.             }
  117.             else
  118.             {
  119.                 DapperConnection.Update(entity);
  120.             }
  121.             return entity;
  122.         }
  123.  
  124.         public override bool Delete(Account entity)
  125.         {
  126.             var res=DapperConnection.Delete(entity);
  127.             return res > 0;
  128.         }
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement