-
Notifications
You must be signed in to change notification settings - Fork 0
/
CarManager.cs
161 lines (143 loc) · 4.96 KB
/
CarManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using Business.Abstract;
using Business.BusinessAspects.Autofac;
using Business.CCS;
using Business.Constants;
using Business.ValidationRules.FluentValidation;
using Core.Aspects.Autofac.Caching;
using Core.Aspects.Autofac.Performance;
using Core.Aspects.Autofac.Transaction;
using Core.Aspects.Autofac.Validation;
using Core.CrossCuttingConcerns.Validation;
using Core.Utilities.Business;
using Core.Utilities.Results;
using DataAccess.Abstract;
using Entities.Concrete;
using Entities.DTOs;
using FluentValidation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Business.Concrete
{
public class CarManager : ICarService
{
ICarDal _carDal;
IBrandService _brandService;
public CarManager(ICarDal carDal, IBrandService brandService)
{
_carDal = carDal;
_brandService = brandService;
}
[SecuredOperation("car.add,admin")]
[ValidationAspect(typeof(CarValidator))]
[CacheRemoveAspect("ICarService.Get")]
public IResult AddCar(Car car)
{
//business code
IResult result = BusinessRules.Run(
CheckIfCarCountOfCategoryCorrect(car.BrandId),
CheckIfCarDescriptionExists(car.DescriptionCar),
CheckIfBrandLimitExceded()
);
if (result != null)
{
return result;
}
_carDal.Add(car);
return new SuccessResult(Messages.CarAdded);
}
public IResult DeleteCar(Car car)
{
_carDal.Delete(car);
return new SuccessResult(Messages.CarDeleted);
}
[ValidationAspect(typeof(CarValidator))]
[CacheRemoveAspect("ICarService.Get")]
public IResult UpdateCar(Car car)
{
IResult result = BusinessRules.Run(CarExists(car.Id));
if (result!=null)
{
return result;
}
_carDal.Update(car);
return new SuccessResult(Messages.CarUpdated);
}
[CacheAspect]
public IDataResult<List<Car>> GetAll()
{
//if (DateTime.Now.Hour == 13)
//{
// return new ErrorDataResult<List<Car>>(Messages.MaintenanceTime);
//}
return new SuccessDataResult<List<Car>>(_carDal.GetAll(), Messages.CarsListed);
}
public IDataResult<List<Car>> GetCarsByBrandId(int id)
{
return new SuccessDataResult<List<Car>>(_carDal.GetAll(c => c.BrandId == id), Messages.CarsListed);
}
public IDataResult<List<Car>> GetCarsByColorId(int id)
{
return new SuccessDataResult<List<Car>>(_carDal.GetAll(c => c.ColorId == id), Messages.CarsListed);
}
[CacheAspect]
//[PerformanceAspect(5)]
public IDataResult<List<Car>> GetById(int carId)
{
return new SuccessDataResult<List<Car>>(_carDal.GetAll(c => c.Id == carId), Messages.CarsListed);
}
private IResult CheckIfCarCountOfCategoryCorrect(int brandId)
{
var result = _carDal.GetAll(c => c.BrandId == brandId).Count;
if (result >= 10)
{
return new ErrorResult(Messages.CarCountOfCategoryError);
}
return new SuccessResult();
}
private IResult CheckIfCarDescriptionExists(string descriptionCar)
{
var result = _carDal.GetAll(c => c.DescriptionCar == descriptionCar).Any();
if (result)
{
return new ErrorResult(Messages.CarDescriptionAlreadyExists);
}
return new SuccessResult();
}
private IResult CheckIfBrandLimitExceded()
{
var result = _brandService.GetAll();
if (result.Data.Count > 15)
{
return new ErrorResult(Messages.BrandLimitExceded);
}
return new SuccessResult();
}
private IResult CarExists(int id)
{
if (_carDal.Exists(c => c.Id == id))
{
return new SuccessResult();
}
return new ErrorResult(Messages.CarNotFound);
}
[TransactionScopeAspect]
public IResult AddTransactionalTest(Car car)
{
throw new NotImplementedException();
}
public IDataResult<List<CarDetailDto2>> GetCarDetails(int id)
{
return new SuccessDataResult<List<CarDetailDto2>>(_carDal.GetCarDetailDtos2(c => c.Id == id), Messages.CarsDetailed);
}
public IDataResult<List<CarDetailDto2>> GetCarDetails2()
{
//if (DateTime.Now.Hour == 17)
//{
// return new ErrorDataResult<List<CarDetailDto>>(Messages.MaintenanceTime);
//}
return new SuccessDataResult<List<CarDetailDto2>>(_carDal.GetCarDetailDtos2(), Messages.SuccessMessage);
}
}
}