Ejemplo n.º 1
0
        public async Task <IActionResult> EditDriver(int id, [FromForm] DriverCreationDto driverDto)
        {
            var driverFromRepo = await _context.Drivers.FirstOrDefaultAsync(x => x.Id == id);

            if (driverFromRepo.Status == "Approved")
            {
                return(BadRequest("You cannot edit an approved License"));
            }

            DriverReturnFiles driverFilesInString = new DriverReturnFiles();

            if (driverDto.Document != null)
            {
                using (var ms = new MemoryStream())
                {
                    driverDto.Document.CopyTo(ms);
                    var fileBytes = ms.ToArray();
                    driverFilesInString.Document = Convert.ToBase64String(fileBytes);
                }
            }

            if (driverDto.OneDayDoc != null)
            {
                using (var ms1 = new MemoryStream())
                {
                    driverDto.OneDayDoc.CopyTo(ms1);
                    var fileBytes = ms1.ToArray();
                    driverFilesInString.OneDayDoc = Convert.ToBase64String(fileBytes);
                }
            }

            if (driverDto.Photo != null)
            {
                using (var ms2 = new MemoryStream())
                {
                    driverDto.Photo.CopyTo(ms2);
                    var fileBytes = ms2.ToArray();
                    driverFilesInString.Photo = Convert.ToBase64String(fileBytes);
                }
            }


            var driverNormalData = _mapper.Map <DriverReturnData>(driverDto);
            var driverFilesData  = _mapper.Map <Driver>(driverFilesInString);

            driverFilesData.Id = driverFromRepo.Id;
            var driverToUpdate = _mapper.Map(driverNormalData, driverFilesData);
            var dataToReturn   = _mapper.Map(driverToUpdate, driverFromRepo);

            if (await _context.SaveChangesAsync() > 0)
            {
                return(Ok(dataToReturn));
            }
            return(BadRequest("Update failed"));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> AddDriver([FromForm] DriverCreationDto driverDto)
        {
            DriverReturnFiles driverFilesDto = new DriverReturnFiles();

            if (driverDto.Document != null)
            {
                using (var ms = new MemoryStream())
                {
                    driverDto.Document.CopyTo(ms);
                    var fileBytes = ms.ToArray();
                    driverFilesDto.Document = Convert.ToBase64String(fileBytes);
                }
            }

            if (driverDto.OneDayDoc != null)
            {
                using (var ms1 = new MemoryStream())
                {
                    driverDto.OneDayDoc.CopyTo(ms1);
                    var fileBytes = ms1.ToArray();
                    driverFilesDto.OneDayDoc = Convert.ToBase64String(fileBytes);
                }
            }

            if (driverDto.Photo != null)
            {
                using (var ms2 = new MemoryStream())
                {
                    driverDto.Photo.CopyTo(ms2);
                    var fileBytes = ms2.ToArray();
                    driverFilesDto.Photo = Convert.ToBase64String(fileBytes);
                }
            }

            var drivertoSave   = _mapper.Map <DriverReturnData>(driverDto);
            var drivertoSave2  = _mapper.Map <Driver>(driverFilesDto);
            var driverToCreate = _mapper.Map(drivertoSave, drivertoSave2);

            await _context.AddAsync(driverToCreate);

            await _context.SaveChangesAsync();

            return(Ok(new{ message = "Created Successfully" }));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> AddDriver([FromForm] DriverCreationDto driverDto)
        {
            var dataWithoutFiles    = _mapper.Map <DriverReturnData>(driverDto);
            var dataWithoutFilesAdd = _mapper.Map <Driver>(dataWithoutFiles);
            await _context.AddAsync(dataWithoutFilesAdd);

            var result = await _context.SaveChangesAsync() > 0;

            if (result == false)
            {
                return(BadRequest("Could not add the driver details"));
            }
            var id = dataWithoutFilesAdd.Id;

            DriverReturnFiles driverFilesDto = new DriverReturnFiles();

            if (driverDto.Document != null)
            {
                string ext      = System.IO.Path.GetExtension(driverDto.Document.FileName);
                string filename = "doc" + id.ToString() + ext;

                string filepath = "wwwroot/assets/" + filename;
                using (var fileStream = new FileStream(filepath, FileMode.Create))
                {
                    await driverDto.Document.CopyToAsync(fileStream);
                }
                driverFilesDto.Document = filename;
            }

            if (driverDto.OneDayDoc != null && driverDto.TrainingPeriod == 1)
            {
                string ext      = System.IO.Path.GetExtension(driverDto.OneDayDoc.FileName);
                string filename = "cer" + id.ToString() + ext;

                string filepath = "wwwroot/assets/" + filename;
                using (var fileStream = new FileStream(filepath, FileMode.Create))
                {
                    await driverDto.OneDayDoc.CopyToAsync(fileStream);
                }
                driverFilesDto.OneDayDoc = filename;
            }

            if (driverDto.Photo != null)
            {
                string ext      = System.IO.Path.GetExtension(driverDto.Photo.FileName);
                string filename = "img" + id.ToString() + ext;

                string filepath = "wwwroot/assets/" + filename;
                using (var fileStream = new FileStream(filepath, FileMode.Create))
                {
                    await driverDto.Photo.CopyToAsync(fileStream);
                }
                driverFilesDto.Photo = filename;
            }

            _mapper.Map(driverFilesDto, dataWithoutFilesAdd);
            result = await _context.SaveChangesAsync() > 0;

            if (result == false)
            {
                await DeleteDriver(dataWithoutFilesAdd.Id);

                return(BadRequest("Error in saving files ,Could not add the driver details"));
            }

            var r = DoPayment(dataWithoutFilesAdd.Id, dataWithoutFiles.Created, dataWithoutFiles.Amount);

            if (await r)
            {
                return(Ok());
            }
            return(BadRequest("Error saving Data"));
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> EditDriver(int id, [FromForm] DriverCreationDto driverDto)
        {
            var driverFromRepo = await _context.Drivers.FirstOrDefaultAsync(x => x.Id == id);

            if (driverFromRepo.Status == "Approved")
            {
                return(BadRequest("You cannot edit an approved License"));
            }

            bool changes = false;

            if (driverFromRepo.TrainingPeriod == 1 && driverDto.TrainingPeriod == 3)
            {
                if (System.IO.File.Exists("wwwroot/assets/" + driverFromRepo.OneDayDoc))
                {
                    System.IO.File.Delete("wwwroot/assets/" + driverFromRepo.OneDayDoc);
                }
            }

            Driver driverToUpdate = new Driver();

            driverToUpdate.Id = driverFromRepo.Id;
            if (driverDto.Document != null)
            {
                if (driverFromRepo.Document != null)
                {
                    if (System.IO.File.Exists("wwwroot/assets/" + driverFromRepo.Document))
                    {
                        System.IO.File.Delete("wwwroot/assets/" + driverFromRepo.Document);
                    }
                }
                string ext      = System.IO.Path.GetExtension(driverDto.Document.FileName);
                string filename = "doc" + driverFromRepo.Id.ToString() + ext;
                string filepath = "wwwroot/assets/" + filename;
                using (var fileStream = new FileStream(filepath, FileMode.Create))
                {
                    await driverDto.Document.CopyToAsync(fileStream);

                    changes = true;
                }
                driverToUpdate.Document = filename;
            }

            if (driverDto.OneDayDoc != null)
            {
                if (driverFromRepo.OneDayDoc != null)
                {
                    if (System.IO.File.Exists("wwwroot/assets/" + driverFromRepo.OneDayDoc))
                    {
                        System.IO.File.Delete("wwwroot/assets/" + driverFromRepo.OneDayDoc);
                    }
                }
                string ext      = System.IO.Path.GetExtension(driverDto.OneDayDoc.FileName);
                string filename = "cer" + driverFromRepo.Id.ToString() + ext;
                string filepath = "wwwroot/assets/" + filename;
                using (var fileStream = new FileStream(filepath, FileMode.Create))
                {
                    await driverDto.OneDayDoc.CopyToAsync(fileStream);

                    changes = true;
                }
                driverToUpdate.OneDayDoc = filename;
            }

            if (driverDto.Photo != null)
            {
                if (driverFromRepo.Photo != null)
                {
                    if (System.IO.File.Exists("wwwroot/assets/" + driverFromRepo.Photo))
                    {
                        System.IO.File.Delete("wwwroot/assets/" + driverFromRepo.Photo);
                    }
                }
                string ext      = System.IO.Path.GetExtension(driverDto.Photo.FileName);
                string filename = "img" + driverFromRepo.Id.ToString() + ext;
                string filepath = "wwwroot/assets/" + filename;
                using (var fileStream = new FileStream(filepath, FileMode.Create))
                {
                    await driverDto.Photo.CopyToAsync(fileStream);

                    changes = true;
                }
                driverToUpdate.Photo = filename;
            }
            if (driverToUpdate.Document == null)
            {
                driverToUpdate.Document = driverFromRepo.Document;
            }
            if (driverToUpdate.OneDayDoc == null)
            {
                driverToUpdate.OneDayDoc = driverFromRepo.OneDayDoc;
            }
            if (driverToUpdate.Photo == null)
            {
                driverToUpdate.Photo = driverFromRepo.Photo;
            }
            var driverNormalData = _mapper.Map <DriverDataWithoutFiles>(driverDto);

            _mapper.Map(driverToUpdate, driverFromRepo);
            var a = _mapper.Map(driverNormalData, driverFromRepo);

            if (await _context.SaveChangesAsync() > 0 || changes)
            {
                var r = (from p in _context.Payments where p.DriverId == id select p).SingleOrDefault();
                r.PaymentAmount = driverDto.Amount;
                await _context.SaveChangesAsync();

                return(Ok(await _context.Drivers.FirstOrDefaultAsync(x => x.Id == id)));
            }

            return(BadRequest("Changes Not Made"));
        }