Beispiel #1
0
        public PersonModule(IRepository repo, IMapper mapper) : base("/api/v1")
        {
            _repo   = repo;
            _mapper = mapper;

            Get("/persons/{id}", parameters =>
            {
                var query   = new FindPersonQuery(parameters.id);
                var handler = PersonQueryHandlerFactory.Build(query, _repo, _mapper);
                return(handler.Get());
            });

            Post("/persons/", parameters =>
            {
                var per             = this.Bind <PersonNew>();
                PersonDetail person = new PersonDetail {
                    Name = per.Name, BirthDay = DateTime.Parse(per.BirthDay)
                };
                var command  = new SavePersonCommand(person);
                var handler  = PersonCommandHandlerFactory.Build(command);
                var responce = handler.Execute();

                if (responce.Success)
                {
                    return(responce);
                }
                return(HttpStatusCode.BadRequest);
            });
        }
Beispiel #2
0
 private void OnCMD(object sender, PropertyChangedEventArgs e)
 {
     if (e.PropertyName.Equals("FirstName") || e.PropertyName.Equals("LastName"))
     {
         SavePersonCommand.RaiseCanExecuteChanged();
     }
 }
Beispiel #3
0
        public PersonModule(IRepository repo, IMapper mapper, ILogger logger) : base("/api/v1")
        {
            _repo   = repo;
            _mapper = mapper;
            _logger = logger;

            Get("/persons/{id}", parameters =>
            {
                var query   = new FindPersonQuery(parameters.id);
                var handler = PersonQueryHandlerFactory.Build(query, _repo, _mapper, _logger);
                _logger.Information("Get request");
                var result = handler.Get();
                if (result == null)
                {
                    return(new NotFoundResponse());
                }
                var res = Encoding.Default.GetBytes(JsonSerializer.Serialize(new PersonDetail
                {
                    BirthDate = result.BirthDate,
                    Name      = result.Name
                }));
                return(new Response
                {
                    ContentType = "application/json",
                    Contents = s => s.Write(res, 0, res.Length)
                });
            });

            Post("/persons/", parameters =>
            {
                var body    = this.Request.Body.AsString();
                var person  = JsonSerializer.Deserialize <Person>(body);
                var command = new SavePersonCommand(person);  //new FindPersonQuery(parameters.id);
                var handler = PersonCommandHandlerFactory.Build(command, _repo, _mapper, _logger);
                var result  = handler.Execute();

                if (result != null)
                {
                    return new Response
                    {
                        StatusCode   = HttpStatusCode.OK,
                        ReasonPhrase = $"Created localhost:1234/persons/{result.ID.ToString()}"
                    }
                }
                ;
                return(new Response
                {
                    StatusCode = HttpStatusCode.Forbidden,
                    ReasonPhrase = "unprocessable entity"
                });
            });
        }
Beispiel #4
0
        public void SavePerson(Guid personId, string name)
        {
            var cmd = new SavePersonCommand(_entity);

            cmd.Run();
        }