-
Notifications
You must be signed in to change notification settings - Fork 0
/
CarImageManager.cs
102 lines (95 loc) · 3.4 KB
/
CarImageManager.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
using Business.Abstract;
using Business.BusinessAspects;
using Business.Constants;
using Core.Results.Utilities;
using Core.Utilities.Business;
using Core.Utilities.Results.DataResults;
using DataAccess.Abstract;
using Entities.Concrete;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Text;
namespace Business.Concrete
{
class CarImageManager : ICarImageService
{
ICarImageDal _carImageDal;
public CarImageManager(ICarImageDal carImageDal)
{
_carImageDal = carImageDal;
}
public string DefaultCarImagePath = "/CarImages/default.jpg";
//[SecuredOperation("admin")]
public IResult Add(CarImage carImage,IFormFile resim)
{
var result = BusinessRules.Run(CarImageCountLessOrEqualThan5(carImage.CarId));
if(result != null)
{
return result;
}
var Upload = FileOperations.UploadImage(resim);
if (Upload.Success)
{
carImage.Date = DateTime.Now;
carImage.ImagePath = Upload.Message;
_carImageDal.Add(carImage);
return new SuccessResult(Messages.Added);
}
return new ErrorResult("Dosya yüklenemedi");
}
//[SecuredOperation("admin")]
public IResult Delete(CarImage carImage)
{
var carimginfo = _carImageDal.Get(p => p.Id == carImage.Id);
if (FileOperations.DeleteImage(carimginfo.ImagePath).Success)
{
_carImageDal.Delete(carImage);
return new SuccessResult(Messages.Deleted);
}
return new ErrorResult();
}
public IDataResult<List<CarImage>> GetAll()
{
return new SuccessDataResult<List<CarImage>>(_carImageDal.GetAll());
}
public IDataResult<List<CarImage>> GetByCarId(int id)
{
var CarImages = _carImageDal.GetAll(p => p.CarId == id);
if(CarImages.Count == 0)
{
CarImage carImage = new CarImage { CarId = id, ImagePath = DefaultCarImagePath};
CarImages.Add(carImage);
}
return new SuccessDataResult<List<CarImage>>(CarImages);
}
public IDataResult<CarImage> GetById(int id)
{
return new SuccessDataResult<CarImage>(_carImageDal.Get(p => p.Id == id));
}
[SecuredOperation("admin")]
public IResult Update(CarImage carImage, IFormFile resim)
{
var Upload = FileOperations.UploadImage(resim);
var CarImageInfo = _carImageDal.Get(p => p.Id == carImage.Id);
if (Upload.Success)
{
FileOperations.DeleteImage(CarImageInfo.ImagePath);
carImage.ImagePath = Upload.Message;
carImage.Date = DateTime.Now;
}
_carImageDal.Update(carImage);
return new SuccessResult(Messages.Updated);
}
public IResult CarImageCountLessOrEqualThan5(int carId)
{
List<CarImage> ResimListesi = _carImageDal.GetAll(c=>c.CarId == carId);
int Resimsayisi = ResimListesi.Count;
if (Resimsayisi >= 5)
{
return new ErrorResult(Messages.ImageCountError);
}
return new SuccessResult();
}
}
}