Ejemplo n.º 1
0
        public async Task <IActionResult> AddModel([FromBody] ModelResource modelResource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var model = mapper.Map <ModelResource, Model>(modelResource);

            context.Add(model);
            await context.SaveChangesAsync();

            return(Ok(modelResource));
        }
        public async Task <IActionResult> CreateVehicle([FromBody] VehicleResource vehicleResource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var vehicle = mapper.Map <VehicleResource, Vehicle>(vehicleResource);

            vehicle.LastUpdate = DateTime.Now;

            context.Add(vehicle);
            await context.SaveChangesAsync();

            return(Ok(vehicle));
        }
        public async Task <IActionResult> UploadPhoto(int vehicleId, IFormFile file)
        {
            var vehicle = await context.Vehicles.SingleOrDefaultAsync(v => v.Id == vehicleId);

            if (vehicle == null)
            {
                return(NotFound());
            }
            if (file == null)
            {
                return(BadRequest("Null file"));
            }

            if (file.Length == 0 || file.Length > photoSettings.MaxBytes)
            {
                return(BadRequest("Wrong File size(over 10mb)"));
            }
            if (!photoSettings.isSupported(file.FileName))
            {
                return(BadRequest("Invalid file type"));
            }

            var uploadsFolderPath = Path.Combine(host.WebRootPath, "uploads");

            if (!Directory.Exists(uploadsFolderPath))
            {
                Directory.CreateDirectory(uploadsFolderPath);
            }
            var fileName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName);
            var filePath = Path.Combine(uploadsFolderPath, fileName);

            using (var stream = new FileStream(filePath, FileMode.Create))
            {
                await file.CopyToAsync(stream);
            }

            var photo = new Photo {
                FileName = fileName
            };

            vehicle.Photos.Add(photo);
            await context.SaveChangesAsync();

            return(Ok(mapper.Map <Photo, PhotoResource>(photo)));
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> UpdateContacts(int id, [FromBody] ContactsResource contactsResource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var contacts = await context.Contacts.SingleOrDefaultAsync(v => v.Id == id);

            mapper.Map <ContactsResource, Contacts>(contactsResource, contacts);
            await context.SaveChangesAsync();

            return(Ok(contactsResource));
        }
        public async Task <IActionResult> Register([FromBody] RegistrationResource registrationResource)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var userIdentity = mapper.Map <ApplicationUser>(registrationResource);
            var result       = await userManager.CreateAsync(userIdentity, registrationResource.Password);

            if (!result.Succeeded)
            {
                return(new BadRequestObjectResult(Errors.AddErrorsToModelState(result, ModelState)));
            }

            await context.CarBuyers.AddAsync(new CarBuyer
            {
                IdentityId = userIdentity.Id,
                Location   = registrationResource.Location
            });

            await context.SaveChangesAsync();

            return(new OkObjectResult("Account created"));
        }