示例#1
0
        public IActionResult Create([FromBody] CreateAthleteDto item)
        {
            try
            {
                Result <AthleteName> athleteNameRequest = AthleteName.Create(item.Name);
                Result <Email>       emailRequest       = Email.Create(item.Email);
                Result result = Result.Combine(athleteNameRequest, emailRequest);
                if (result.IsFailure)
                {
                    return(BadRequest(result.Error));
                }

                if (athleteRepository.GetByEmail(emailRequest.Value) != null)
                {
                    return(BadRequest("Email is already in use: " + item.Email));
                }

                var athlete = new Athlete(athleteNameRequest.Value, emailRequest.Value);
                athleteRepository.Add(athlete);
                athleteRepository.SaveChanges();

                return(Ok());
            }
            catch (Exception e)
            {
                return(StatusCode(500, new { error = e.Message }));
            }
        }
示例#2
0
        public IActionResult Update(long id, [FromBody] UpdateAthleteDto item)
        {
            try
            {
                Result <AthleteName> athleteNameRequest = AthleteName.Create(item.Name);
                if (athleteNameRequest.IsFailure)
                {
                    return(BadRequest(athleteNameRequest.Error));
                }

                Athlete athlete = athleteRepository.GetById(id);
                if (athlete == null)
                {
                    return(BadRequest("Invalid athlete Id: " + id));
                }

                athlete.Name = athleteNameRequest.Value;
                athleteRepository.SaveChanges();

                return(Ok());
            }
            catch (Exception e)
            {
                return(StatusCode(500, new { error = e.Message }));
            }
        }