| [752] | 1 | using System;
|
|---|
| 2 | using System.Collections.Generic;
|
|---|
| 3 | using System.Diagnostics;
|
|---|
| 4 | using System.Linq;
|
|---|
| 5 | using System.Linq.Expressions;
|
|---|
| 6 | using System.Text;
|
|---|
| 7 | using Wierszowki.Core;
|
|---|
| 8 | using Wierszowki.Core.Interfaces;
|
|---|
| 9 | using Wierszowki.Core.Linq;
|
|---|
| 10 | using Wierszowki.Services.Interfaces;
|
|---|
| 11 | using xVal.ServerSide;
|
|---|
| 12 |
|
|---|
| 13 | namespace Wierszowki.Services
|
|---|
| 14 | {
|
|---|
| 15 | public class UserService : IUserService
|
|---|
| 16 | {
|
|---|
| 17 | private readonly IRepository<User> _repository;
|
|---|
| 18 | public UserService()
|
|---|
| 19 | {
|
|---|
| 20 | _repository = new LinqRepository<User>();
|
|---|
| 21 | }
|
|---|
| 22 |
|
|---|
| 23 | public UserService(IRepository<User> repository)
|
|---|
| 24 | {
|
|---|
| 25 | _repository = repository;
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 | public bool LoginUser(string login, string password)
|
|---|
| 30 | {
|
|---|
| 31 | return _repository.Exists(u => u.Login == login && u.Password == password);
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | public void Create(User user)
|
|---|
| 35 | {
|
|---|
| 36 | var errors = DataAnnotationsValidationRunner.GetErrors(user);
|
|---|
| 37 | if (errors.Any())
|
|---|
| 38 | throw new RulesException(errors);
|
|---|
| 39 |
|
|---|
| 40 | //TODO: check if the user with given login name exists...
|
|---|
| 41 |
|
|---|
| 42 | _repository.Insert(user);
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | public void Update(User user)
|
|---|
| 46 | {
|
|---|
| 47 | var errors = DataAnnotationsValidationRunner.GetErrors(user);
|
|---|
| 48 | if (errors.Any())
|
|---|
| 49 | throw new RulesException(errors);
|
|---|
| 50 |
|
|---|
| 51 | var userToUpdate = _repository.Find(user.Id).Single();
|
|---|
| 52 | userToUpdate.FirstName = user.FirstName;
|
|---|
| 53 | userToUpdate.LastName = user.LastName;
|
|---|
| 54 | userToUpdate.Login = user.Login;
|
|---|
| 55 | userToUpdate.Password = user.Password;
|
|---|
| 56 |
|
|---|
| 57 | _repository.Update(userToUpdate);
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | public void Delete(User user)
|
|---|
| 61 | {
|
|---|
| 62 | _repository.Delete(user);
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | public User Find(int id)
|
|---|
| 66 | {
|
|---|
| [838] | 67 | return _repository.Find(id).SingleOrDefault();
|
|---|
| [752] | 68 | }
|
|---|
| 69 |
|
|---|
| 70 | public User FindOne(int id)
|
|---|
| 71 | {
|
|---|
| 72 | return _repository.FindOne(id);
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | public User FindOne(Expression<Func<User, bool>> expression)
|
|---|
| 76 | {
|
|---|
| 77 | return _repository.FindOne(expression);
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | public IList<User> FindAll()
|
|---|
| 81 | {
|
|---|
| 82 | return _repository.FindAll().ToList();
|
|---|
| 83 | }
|
|---|
| 84 | }
|
|---|
| 85 | } |
|---|