forked from ardalis/Result
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPersonService.cs
More file actions
51 lines (40 loc) · 1.37 KB
/
Copy pathPersonService.cs
File metadata and controls
51 lines (40 loc) · 1.37 KB
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
using Ardalis.Result.Sample.Core.Model;
using Ardalis.Result.Sample.Core.Validators;
using Ardalis.Result.FluentValidation;
using System.Linq;
namespace Ardalis.Result.Sample.Core.Services
{
public class PersonService
{
private readonly int[] _knownIds = new [] { 1 };
private readonly Person _existPerson = new() { Forename = "John", Surname = "Smith" };
public Result<Person> Create(string firstName, string lastName)
{
var person = new Person
{
Forename = firstName,
Surname = lastName
};
var validator = new PersonValidator();
var result = validator.Validate(person);
if (!result.IsValid)
{
return Result.Invalid(result.AsErrors());
}
if (person.Forename == _existPerson.Forename && person.Surname == _existPerson.Surname)
{
return Result.Conflict($"Person ({person.Forename} {person.Surname}) is exist");
}
return Result.Success(person);
}
public Result Remove(int id)
{
if (!_knownIds.Any(knownId => knownId == id))
{
return Result.NotFound("Person Not Found");
}
//Pretend removing person
return Result.Success();
}
}
}