예제 #1
0
        public async Task <IActionResult> LeaveComment([FromBody] DriverCommentCreationDto comment)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var driver = _usersRepository.GetDriverById(comment.DriverId);

            if (driver == null || string.IsNullOrEmpty(comment.Message))
            {
                return(NotFound());
            }
            var customerId = Guid.Parse(User.Claims.FirstOrDefault(c => c.Type == Helpers.Constants.Strings.JwtClaimIdentifiers.CustomerId)?.Value);

            var customerIds = _tripsRepository.GetCustomerIdsForDriverTrips(comment.DriverId);

            if (!customerIds.Any(id => id == customerId))
            {
                ModelState.AddModelError(nameof(DriverCommentCreationDto), "No trips with driver");
                return(BadRequest(ModelState));
            }

            var commentEntity = new DriverComment()
            {
                CustomerId   = customerId,
                CreationTime = DateTime.UtcNow,
                Message      = comment.Message
            };

            driver.DriverComments.Add(commentEntity);

            if (!await _usersRepository.UpdateDriver(driver))
            {
                return(Conflict());
            }

            var commentDto = Mapper.Map <DriverCommentDto>(commentEntity);

            return(Ok(commentDto));
        }