Example #1
0
        public async Task <IActionResult> Register([FromBody] RegisterViewModel model)
        {
            string strErrors;

            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email
                };
                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    _logger.LogInformation($"User {model.Email} created a new account with password.");
                    return(Json(_tokenService.GenerateToken(model.Email)));
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
                strErrors = _helperService.GetModelStateErrorsString(ModelState);
                return(BadRequest(strErrors));
            }
            // ModelState is invalid.
            strErrors = _helperService.GetModelStateErrorsString(ModelState);
            return(BadRequest(strErrors));
        }
Example #2
0
        public IActionResult AddPhoto(string title, string description, IFormFile file)
        {
            Photo p = new Photo
            {
                Title        = title,
                Description  = description,
                UserName     = User.Identity.Name,
                CreatedDate  = DateTime.Now,
                ModifiedDate = DateTime.Now
            };

            if (file != null && file.Length > 0)
            {
                p.FileName      = file.FileName;
                p.ImageMimeType = file.ContentType;
                p.FileData      = new byte[file.Length];
                using (var stream = file.OpenReadStream())
                {
                    stream.Read(p.FileData, 0, (int)file.Length);
                }
                // Add the photo to the database.
                _dbContext.Photos.Add(p);
                _dbContext.SaveChanges();
                return(Ok());
            }

            ModelState.AddModelError("", "File is not received by the server!");
            string strErrors = _helperService.GetModelStateErrorsString(ModelState);

            return(BadRequest(strErrors));
        }