/Gradebook.Data/Repositories/AccountRepository.cs
C# | 96 lines | 79 code | 17 blank | 0 comment | 9 complexity | c7b6a3d424c10af1e01452d2306deb5a MD5 | raw file
- using System;
- using System.Collections.Generic;
- using System.Data.Entity;
- using System.Linq;
- using Gradebook.Contracts.Repositories;
- using Gradebook.Model;
-
- namespace Gradebook.Data.Repositories
- {
- public class AccountRepository : Repository<User>, IAccountRepository
- {
- public AccountRepository(DbContext context) : base(context) { }
-
- public override User Read(int id)
- {
- if (!Contains(id))
- throw new ArgumentException("id");
-
- return DbSet.Include(x => x.Teacher).Single(x => x.Id == id);
- }
-
- public User Create(string name, string password, string email)
- {
- return Create(name, password, email, UserRole.Teacher);
- }
-
- public User Create(string name, string password, string email, UserRole role)
- {
- var user = new User
- {
- Name = name,
- Password = password,
- Email = email,
- Role = role
- };
-
- Create(user);
- return user;
- }
-
- public User GetUserByName(string username)
- {
- if (!Contains(username))
- throw new ArgumentException("username");
-
- return DbSet.Include(x => x.Teacher).Single(x => x.Name == username);
- }
-
- public IEnumerable<User> GetAll(string roleName)
- {
- var role = ToRole(roleName);
- return DbSet.Where(x => x.Role == role);
- }
-
- public bool Contains(string userName)
- {
- return DbSet.Any(x => x.Name == userName);
- }
-
- public static UserRole ToRole(string roleName)
- {
- UserRole role;
- if (!Enum.TryParse(roleName, out role)) throw new ArgumentException("roleName");
- return role;
- }
-
- #region Implementation of IUnitOfWork
-
- public void Commit()
- {
- DbContext.SaveChanges();
- }
-
- private bool _disposed = false;
-
- protected virtual void Dispose(bool disposing)
- {
- if (!_disposed)
- {
- if (disposing)
- {
- DbContext.Dispose();
- }
- }
- _disposed = true;
- }
-
- public void Dispose()
- {
- Dispose(true);
- GC.SuppressFinalize(this);
- }
-
- #endregion
- }
- }