public async Task <IActionResult> AddVehicleMaintenanceAsync([FromBody] AddVehicleMaintenanceManagementAc addVehicleMaintenance)
        {
            var user = await _userManager.FindByNameAsync(User.Identity.Name);

            return(Ok(await _vehicleMaintenanceManagementRepository.AddVehicleMaintenanceAsync(addVehicleMaintenance, user)));
        }
Beispiel #2
0
        /// <summary>
        /// Method to add Vehicle Maintenance - SS
        /// </summary>
        /// <param name="addVehicleMaintenance">Vehicle Maintenance</param>
        /// <param name="loggedInUser">logged in user</param>
        /// <returns>response</returns>
        public async Task <VehicleMaintenanceManagementResponse> AddVehicleMaintenanceAsync(AddVehicleMaintenanceManagementAc
                                                                                            addVehicleMaintenance, ApplicationUser loggedInUser)
        {
            if (string.IsNullOrEmpty(addVehicleMaintenance.Code.Trim()))
            {
                return new VehicleMaintenanceManagementResponse()
                       {
                           HasError = true, Message = "Code can't be empty", ErrorType = VehicleMaintenanceManagementResponseType.Code
                       }
            }
            ;
            else if (string.IsNullOrEmpty(addVehicleMaintenance.ActionTaken.Trim()))
            {
                return new VehicleMaintenanceManagementResponse()
                       {
                           HasError = true, Message = "Action taken can't be empty", ErrorType = VehicleMaintenanceManagementResponseType.ActionTaken
                       }
            }
            ;
            else
            {
                var instituteId = await _instituteUserMappingHelperService.GetUserCurrentSelectedInstituteIdAsync(loggedInUser.Id, true);

                if (!await _iMSDbContext.VehicleMasters.AnyAsync(x => x.InstituteId == instituteId && x.Id == addVehicleMaintenance.VehicleId))
                {
                    return new VehicleMaintenanceManagementResponse()
                           {
                               HasError = true, Message = "Vehicle not found", ErrorType = VehicleMaintenanceManagementResponseType.VehicleId
                           }
                }
                ;
                else
                {
                    if (await _iMSDbContext.VehicleMaintenances.AnyAsync(x => x.Code.ToLowerInvariant() == addVehicleMaintenance.Code.ToLowerInvariant() && x.Vehicle.InstituteId == instituteId))
                    {
                        return new VehicleMaintenanceManagementResponse()
                               {
                                   HasError = true, Message = "Vehicle maintenance code already exist. Please unique one", ErrorType = VehicleMaintenanceManagementResponseType.Code
                               }
                    }
                    ;
                    else
                    {
                        var maintenance = new VehicleMaintenance()
                        {
                            ActionTaken         = addVehicleMaintenance.ActionTaken,
                            Code                = addVehicleMaintenance.Code,
                            CreatedOn           = DateTime.UtcNow,
                            EstimateCost        = addVehicleMaintenance.EstimateCost,
                            MaintenanceDate     = addVehicleMaintenance.MaintenanceDate,
                            MaintenanceDoneBy   = addVehicleMaintenance.MaintenanceDoneBy,
                            NextMaintenanceDate = addVehicleMaintenance.NextMaintenanceDate,
                            Remark              = addVehicleMaintenance.Remark,
                            UpdatedById         = loggedInUser.Id,
                            UpdatedOn           = DateTime.UtcNow,
                            VehicleId           = addVehicleMaintenance.VehicleId
                        };

                        _iMSDbContext.VehicleMaintenances.Add(maintenance);
                        await _iMSDbContext.SaveChangesAsync();

                        return(new VehicleMaintenanceManagementResponse()
                        {
                            HasError = false, Message = "Vehicle maintenance added successfully"
                        });
                    }
                }
            }
        }