| 1 | using System;
|
|---|
| 2 | using System.Collections.Generic;
|
|---|
| 3 | using System.Linq;
|
|---|
| 4 | using System.Text;
|
|---|
| 5 | using Wierszowki.Core.Interfaces;
|
|---|
| 6 | using Wierszowki.Core.Linq;
|
|---|
| 7 | using xVal.ServerSide;
|
|---|
| 8 |
|
|---|
| 9 | namespace Wierszowki.Services
|
|---|
| 10 | {
|
|---|
| 11 | public class MagazineItemService
|
|---|
| 12 | {
|
|---|
| 13 | private readonly IRepository<MagazineItem> _repository;
|
|---|
| 14 | private readonly IRepository<User> _userRepository;
|
|---|
| 15 |
|
|---|
| 16 | public MagazineItemService()
|
|---|
| 17 | {
|
|---|
| 18 | _repository = new LinqRepository<MagazineItem>();
|
|---|
| 19 | _userRepository = new LinqRepository<User>();
|
|---|
| 20 | }
|
|---|
| 21 |
|
|---|
| 22 | public MagazineItemService(IRepository<MagazineItem> repository, IRepository<User> userRepository)
|
|---|
| 23 | {
|
|---|
| 24 | _repository = repository;
|
|---|
| 25 | _userRepository = userRepository;
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | public MagazineItem Find(int id)
|
|---|
| 29 | {
|
|---|
| 30 | return _repository.Find(id).SingleOrDefault();
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | public void Create(MagazineItem magazineItem, string currentUser)
|
|---|
| 34 | {
|
|---|
| 35 |
|
|---|
| 36 | var errors = DataAnnotationsValidationRunner.GetErrors(magazineItem);
|
|---|
| 37 | if (errors.Any())
|
|---|
| 38 | {
|
|---|
| 39 | throw new RulesException(errors);
|
|---|
| 40 | }
|
|---|
| 41 | var user = _userRepository.FindOne(u => u.Login == currentUser);
|
|---|
| 42 | var now = DateTime.Now;
|
|---|
| 43 | magazineItem.CreatedBy = user.Id;
|
|---|
| 44 | magazineItem.CreatedOn = now;
|
|---|
| 45 | magazineItem.UpdatedBy = user.Id;
|
|---|
| 46 | magazineItem.UpdatedOn = now;
|
|---|
| 47 |
|
|---|
| 48 | _repository.Insert(magazineItem);
|
|---|
| 49 |
|
|---|
| 50 | }
|
|---|
| 51 | public void Update(MagazineItem magazineItem, string currentUser)
|
|---|
| 52 | {
|
|---|
| 53 | var errors = DataAnnotationsValidationRunner.GetErrors(magazineItem);
|
|---|
| 54 | if (errors.Any())
|
|---|
| 55 | throw new RulesException(errors);
|
|---|
| 56 |
|
|---|
| 57 | var user = _userRepository.FindOne(u => u.Login == currentUser);
|
|---|
| 58 | var now = DateTime.Now;
|
|---|
| 59 |
|
|---|
| 60 | var magazineItemToUpdate = _repository.FindOne(magazineItem.Id);
|
|---|
| 61 | magazineItemToUpdate.AuthorId = magazineItem.AuthorId;
|
|---|
| 62 | magazineItemToUpdate.Bonus = magazineItem.Bonus;
|
|---|
| 63 | magazineItemToUpdate.Caption = magazineItem.Caption;
|
|---|
| 64 | magazineItemToUpdate.Date = magazineItem.Date;
|
|---|
| 65 | magazineItemToUpdate.Description = magazineItem.Description;
|
|---|
| 66 | magazineItemToUpdate.IssueId = magazineItem.IssueId;
|
|---|
| 67 | magazineItemToUpdate.ItemTypeId = magazineItem.ItemTypeId;
|
|---|
| 68 | magazineItemToUpdate.Price = magazineItem.Price;
|
|---|
| 69 | magazineItemToUpdate.UpdatedBy = user.Id;
|
|---|
| 70 | magazineItemToUpdate.UpdatedOn = now;
|
|---|
| 71 | _repository.Update(magazineItemToUpdate);
|
|---|
| 72 | }
|
|---|
| 73 | public MagazineItem Delete(MagazineItem magazineItem)
|
|---|
| 74 | {
|
|---|
| 75 | var magazineItemToDelete = _repository.FindOne(magazineItem.Id);
|
|---|
| 76 | if (magazineItemToDelete != null)
|
|---|
| 77 | {
|
|---|
| 78 | _repository.Delete(magazineItemToDelete);
|
|---|
| 79 | }
|
|---|
| 80 | return magazineItemToDelete;
|
|---|
| 81 | }
|
|---|
| 82 | }
|
|---|
| 83 | }
|
|---|