Last active
January 31, 2021 13:24
-
-
Save umayeras/79228ab26cf1c03ed490a77a17c5d82c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface ISampleService | |
{ | |
ServiceDataResult GetAll(); | |
ServiceDataResult Get(string id); | |
ServiceResult Add(AddSampleRequest request); | |
ServiceResult Update(UpdateSampleRequest request); | |
ServiceResult Delete(string id); | |
} | |
public class SampleService : ISampleService | |
{ | |
private readonly ISampleRepository repository; | |
private readonly ISampleFactory factory; | |
public SampleService(ISampleRepository repository, ISampleFactory factory) | |
{ | |
this.repository = repository; | |
this.factory = factory; | |
} | |
public ServiceDataResult GetAll() | |
{ | |
var samples = repository.AsQueryable() | |
.Select(p => new {p.Id, p.Title}) | |
.ToList(); | |
return ServiceDataResult.Success(samples); | |
} | |
public ServiceDataResult Get(string id) | |
{ | |
var sample = repository.FindById(id); | |
return ServiceDataResult.Success(sample); | |
} | |
public ServiceResult Add(AddSampleRequest request) | |
{ | |
var sample = factory.CreateAddSample(request); | |
var result = repository.Insert(sample); | |
return string.IsNullOrEmpty(result) | |
? ServiceResult.Error(Messages.AddingFailed) | |
: ServiceResult.Success(Messages.AddingSuccess); | |
} | |
public ServiceResult Update(UpdateSampleRequest request) | |
{ | |
var currentSample = repository.FindById(request.Id); | |
var sample = factory.CreateUpdateSample(currentSample, request); | |
var result = repository.ReplaceOne(sample); | |
return !result | |
? ServiceResult.Error(Messages.UpdatingFailed) | |
: ServiceResult.Success(Messages.UpdatingSuccess); | |
} | |
public ServiceResult Delete(string id) | |
{ | |
var result = repository.DeleteById(id); | |
return !result | |
? ServiceResult.Error(Messages.DeletingFailed) | |
: ServiceResult.Success(Messages.DeletingSuccess); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment