コード例 #1
0
        public async Task <IActionResult> Post([FromForm] BrandViewModel model, [FromForm] IFormFile brandIcon)
        {
            if (ModelState.IsValid)
            {
                // Check the file
                if (brandIcon == null)
                {
                    return(BadRequest("Brand icon file is required"));
                }

                var fileName          = brandIcon.FileName;
                var extension         = Path.GetExtension(fileName);
                var allowedExtensions = new List <string>
                {
                    ".png", ".jpg", ".bmp"
                };

                // Check the extension
                if (!allowedExtensions.Contains(extension))
                {
                    return(BadRequest("Please upload a valid image file"));
                }

                string newFileName = $"images/{Guid.NewGuid().ToString()}{extension}";

                using (var fileStream = new FileStream($"{Directory.GetCurrentDirectory()}/wwwroot/{newFileName}", FileMode.Create, FileAccess.Write))
                {
                    await brandIcon.CopyToAsync(fileStream);
                }

                var brand = new Brand
                {
                    ID          = Guid.NewGuid().ToString(),
                    Country     = model.Country,
                    CreatedDate = DateTime.UtcNow,
                    Name        = model.Name,
                    IconPath    = newFileName
                };

                await _db.Brands.AddAsync(brand);

                await _db.SaveChangesAsync();

                brand.IconPath = $"{_configuration["WebsiteUrl"]}{brand.IconPath}";
                return(Ok(new HttpSingleResponse <Brand>
                {
                    IsSuccess = true,
                    Message = "Brand added successfully!",
                    Value = brand
                }));
            }

            return(BadRequest("Some properties are not valid"));
        }
コード例 #2
0
        public async Task <IActionResult> Post(BookingViewModel model)
        {
            if (ModelState.IsValid)
            {
                var car = await _db.Cars.FindAsync(model.CarId);

                if (car == null)
                {
                    return(NotFound());
                }

                var customer = new Customer();
                customer.FirstName   = model.FirstName;
                customer.LastName    = model.LastName;
                customer.Address     = model.Address;
                customer.CreatedDate = DateTime.UtcNow;
                customer.ID          = Guid.NewGuid().ToString();
                customer.Email       = model.Email;
                customer.Phone       = model.Phone;
                var booking = new Booking
                {
                    ID          = Guid.NewGuid().ToString(),
                    CarId       = model.CarId,
                    CreatedDate = DateTime.UtcNow,
                    Time        = model.Time,
                    Description = model.Description,
                };
                customer.Bookings = new List <Booking>();
                customer.Bookings.Add(booking);
                await _db.Customers.AddAsync(customer);

                await _db.SaveChangesAsync();

                string message = $"{car.Model} has been booked for test driver by {model.FirstName} {model.LastName}";
                await _hub.Clients.All.SendAsync("booking", message);

                return(Ok(new HttpSingleResponse <string>
                {
                    Message = "Booking has been received successfully!",
                    IsSuccess = true,
                    ResponseDate = DateTime.UtcNow,
                    Value = message
                }));
            }

            return(BadRequest("Some properties are not valid"));
        }
コード例 #3
0
        public async Task <IActionResult> Post([FromForm] CarViewModel model, [FromForm] IFormFile carImage)
        {
            if (ModelState.IsValid)
            {
                // Validate the price
                if (model.Price < 0)
                {
                    return(BadRequest("Price must be bigger than 0"));
                }

                // Get the brand and type
                var type = await _db.CarTypes.FindAsync(model.TypeId);

                if (type == null)
                {
                    return(BadRequest("Invalid type ID"));
                }

                var brand = await _db.Brands.FindAsync(model.BrandId);

                if (brand == null)
                {
                    return(BadRequest("Invalid brand ID"));
                }

                string carImagePath = "images/default.jpg";

                if (carImage != null)
                {
                    string extension = Path.GetExtension(carImage.FileName);


                    // Check the extension
                    if (!allowedExtensions.Contains(extension))
                    {
                        return(BadRequest("Please upload a valid image file"));
                    }

                    string newFileName = $"images/{Guid.NewGuid().ToString()}{extension}";

                    using (var fileStream = new FileStream($"{Directory.GetCurrentDirectory()}/wwwroot/{newFileName}", FileMode.Create, FileAccess.Write))
                    {
                        await carImage.CopyToAsync(fileStream);
                    }

                    carImagePath = newFileName;
                }

                var car = await _db.Cars.AddAsync(new Car
                {
                    ID          = Guid.NewGuid().ToString(),
                    BrandId     = model.BrandId,
                    CarTypeId   = model.TypeId,
                    Description = model.Description?.Trim(),
                    CreatedDate = DateTime.UtcNow,
                    PhotoPath   = carImagePath,
                    Price       = model.Price,
                    Model       = model.CarModel,
                    Year        = model.Year,
                });

                await _db.SaveChangesAsync();

                return(Ok(new HttpSingleResponse <Car>
                {
                    IsSuccess = true,
                    Message = "Car added successfully!",
                    Value = new Car
                    {
                        Model = car.Entity.Model,
                        Year = car.Entity.Year,
                        BrandId = car.Entity.BrandId,
                        CarTypeId = car.Entity.CarTypeId,
                        Brand = new Brand
                        {
                            Name = brand.Name,
                        },
                        ID = car.Entity.ID,
                        Description = car.Entity.Description,
                        Price = car.Entity.Price,
                        PhotoPath = $"{_configuration["WebsiteUrl"]}{car.Entity.PhotoPath}",
                    }
                }));
            }

            return(BadRequest("Some properties are not valid"));
        }