public async Task <IActionResult> PutCustomers([FromRoute] int id, [FromBody] Customers customers)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            /*if(_context.Customers.Any(c => c.Id == id))
             * {
             *  return NotFound();
             * }
             *
             * _context.Customers.Update(customers);
             * await _context.SaveChangesAsync();*/

            var currentCustomer = await _context.Customers.Where(c => c.Id == id).FirstOrDefaultAsync();

            if (currentCustomer == null)
            {
                return(BadRequest());
            }

            if (customers.Name == null && customers.Surname == null)
            {
                currentCustomer.UpdateBy = User.Identity.Name;
            }
            else if (customers.Name == null && customers.Surname != null)
            {
                currentCustomer.Surname  = customers.Surname;
                currentCustomer.UpdateBy = User.Identity.Name;
            }
            else if (customers.Surname == null && customers.Name != null)
            {
                currentCustomer.Name     = customers.Name;
                currentCustomer.UpdateBy = User.Identity.Name;
            }
            else
            {
                currentCustomer.Name     = customers.Name;
                currentCustomer.Surname  = customers.Surname;
                currentCustomer.UpdateBy = User.Identity.Name;
            }
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CustomersExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }
            return(NoContent());
        }
        public async Task <IActionResult> PutUsers([FromRoute] int id, [FromBody] Users users)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if ((_context.Users.Where(c => c.Id != id && c.Username == users.Username)).Count() > 0)
            {
                return(BadRequest("Username Already Used"));
            }

            /*if (_context.Users.Any(u => u.Id == id))
             * {
             *  return NotFound();
             * }*/

            var currentUser = await _context.Users.Where(c => c.Id == id).FirstOrDefaultAsync();

            if (currentUser == null)
            {
                return(BadRequest());
            }

            if (String.IsNullOrEmpty(users.Username) && String.IsNullOrEmpty(users.Password))
            {
                currentUser.Role = users.Role;
            }
            else if (String.IsNullOrEmpty(users.Username) && String.IsNullOrEmpty(users.Role))
            {
                currentUser.Password = users.Password;
            }
            else if (String.IsNullOrEmpty(users.Password) && String.IsNullOrEmpty(users.Role))
            {
                currentUser.Username = users.Username;
            }
            else if (String.IsNullOrEmpty(users.Username))
            {
                currentUser.Password = users.Password;
                currentUser.Role     = users.Role;
            }
            else if (String.IsNullOrEmpty(users.Password))
            {
                currentUser.Username = users.Username;
                currentUser.Role     = users.Role;
            }
            else if (String.IsNullOrEmpty(users.Role))
            {
                currentUser.Username = users.Username;
                currentUser.Password = users.Password;
            }
            else
            {
                currentUser.Username = users.Username;
                currentUser.Password = users.Password;
                currentUser.Role     = users.Role;
            }

            //_context.Entry(currentUser).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!UsersExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Exemple #3
0
        public async Task <IActionResult> PostImage([FromRoute] int id, IFormFile file)
        {
            string [] validFileExtensions = { ".png", ".jpg", ".tiff", ".bmp" };

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            long   size          = file.Length;
            string fileExtension = "." + file.FileName.Split('.')[file.FileName.Split('.').Length - 1];

            if (!validFileExtensions.Contains(fileExtension))
            {
                return(NoContent());
            }

            var directory   = _hostingEnvironment.WebRootPath;
            var directoryDB = _configuration.GetSection("ImagesDirectory").Value;

            if (!Directory.Exists(directory + directoryDB))
            {
                Directory.CreateDirectory(directory + directoryDB);
            }

            // full path to file, wwwroot and imagesDirectory
            var filePath = Path.Combine(directoryDB, Path.GetRandomFileName());

            filePath = filePath.Split('.')[filePath.Split('.').Length - 2];
            filePath = filePath + fileExtension;
            var currentCustomer = await _context.Customers.Where(c => c.Id == id).FirstOrDefaultAsync();

            if (currentCustomer == null)
            {
                return(BadRequest());
            }

            if (String.IsNullOrEmpty(currentCustomer.Image) && String.IsNullOrWhiteSpace(currentCustomer.Image))
            {
                DeleteFile(currentCustomer.Image);
            }

            currentCustomer.UpdateBy = User.Identity.Name;
            currentCustomer.Image    = filePath;

            try
            {
                await _context.SaveChangesAsync();

                if (file.Length > 0)
                {
                    using (var stream = new FileStream(directory + filePath, FileMode.Create))
                    {
                        await file.CopyToAsync(stream);
                    }
                }
                return(Ok(new { size, filePath }));
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CustomersExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }
        }