Source

main / Source / WebClient / Web.DataLayer / UserRepository.cs

Full commit
using System.Linq;

namespace DataUp.Web.DataLayer
{
    /// <summary>
    /// Class representing user repository
    /// </summary>
    public class UserRepository : RepositoryBase<User>, IUserRepository
    {
        private readonly DataUpEntities dataUpEntities ;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="dataUpEntities">dataUp entities</param>
        public UserRepository(DataUpEntities dataUpEntities)
            : base(dataUpEntities)
        {
            this.dataUpEntities = dataUpEntities;
        }

        /// <summary>
        /// Fetch user from repository
        /// </summary>
        /// <param name="nameIdentifier">name identifier for user to be fetched</param>
        /// <param name="identityProvider">identity provider for user to be fetched</param>
        /// <returns>User to be fetched</returns>
        public User GetUser(string nameIdentifier, string identityProvider)
        {
            var users = this.dataUpEntities.Users.Where(u => u.NameIdentifier == nameIdentifier && u.IdentityProvider == identityProvider);
            var user = users.Any() ? users.Single() : null;
            return user;
        }

        /// <summary>
        /// Add a user to repository
        /// </summary>
        /// <param name="user">user to be added</param>
        public void AddUser(User user)
        {
            this.dataUpEntities.Users.AddObject(user);
            this.dataUpEntities.SaveChanges();
        }
    }
}