예제 #1
0
        public async Task <IActionResult> Put(
            [Required]
            int id,
            [FromForm]
            BindWebsite updatedWebsite,
            [FromForm]
            [MaxFileSizeInMegabytes(2)]
            [OnlyJpegAndPngAllowed]
            IFormFile homepageSnapshot)
        {
            Website website = await _context
                              .Websites
                              .Include(w => w.Category)
                              .Include(w => w.HomepageSnapshot)
                              .Include(w => w.Login)
                              .FirstOrDefaultAsync(w => w.Id == id && w.IsDeleted == false);

            website.Name = updatedWebsite.Name;
            website.URL  = updatedWebsite.URL;
            website.Name = updatedWebsite.Name;

            if (website.Category.Id != updatedWebsite.CategoryId)
            {
                WebsiteCategory category = await _context.Categories.FirstOrDefaultAsync(cat => cat.Id == updatedWebsite.CategoryId);

                if (category == null)
                {
                    throw new ArgumentOutOfRangeException("CategoryId");
                }

                website.Category = category;
            }

            if (website.Login.Email != updatedWebsite.LoginEmail ||
                _passwordSafe.Decrypt(website.Login.Password) != updatedWebsite.LoginPassword)
            {
                website.Login = new WebsiteCredentials()
                {
                    Email    = updatedWebsite.LoginEmail,
                    Password = _passwordSafe.Encrypt(updatedWebsite.LoginPassword)
                };
            }

            if (homepageSnapshot != null)
            {
                HomepageSnapshot snapshot = await _saveSnapshot.SaveAsync(homepageSnapshot);

                website.HomepageSnapshot = snapshot;
            }

            _context.Update(website);
            await _context.SaveChangesAsync();

            return(NoContent());
        }
예제 #2
0
        public async Task <IActionResult> Post(
            [FromForm] BindWebsite website,
            [Required]
            [FromForm]
            [MaxFileSizeInMegabytes(2)]
            [OnlyJpegAndPngAllowed]
            IFormFile homepageSnapshot)
        {
            try
            {
                HomepageSnapshot newHomepageSnapshot = await _saveSnapshot.SaveAsync(homepageSnapshot);

                WebsiteCategory category = await _context.Categories.FirstOrDefaultAsync(cat => cat.Id == website.CategoryId);

                string encryptedPassword = _passwordSafe.Encrypt(website.LoginPassword);
                var    newWebsite        = new Website()
                {
                    Name             = website.Name,
                    URL              = website.URL,
                    Category         = category,
                    HomepageSnapshot = newHomepageSnapshot,
                    Login            = new WebsiteCredentials()
                    {
                        Email    = website.LoginEmail,
                        Password = encryptedPassword
                    }
                };

                _context.Websites.Add(newWebsite);
                await _context.SaveChangesAsync();

                return(Created(
                           UriHelper.BuildAbsolute(
                               Request.Scheme,
                               Request.Host,
                               Request.PathBase,
                               Request.Path.Add("/" + newWebsite.Id.ToString())),
                           OutputWebsite.FromWebsiteEntityWithFullImageUrl(
                               newWebsite,
                               Request.Scheme,
                               Request.Host,
                               _passwordSafe.Decrypt(newWebsite.Login.Password))));
            }catch (Exception ex)
            {
                //ToDo: log exception
                throw ex;
            }
        }