Ejemplo n.º 1
0
        public async Task <ActionResult> Register([FromBody] UserModel user)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState.ValidationState));
            }

            Tuple <JwtModel, string> model;
            User users;

            try
            {
                user.Email = user.Email.ToLower();
                var verify = await _userContext.EmailExists(user.Email);

                if (verify)
                {
                    return(BadRequest(new { Email = "Email Already exist" }));
                }

                model = await _userContext.RegisterUser(user);

                if (user.Photo != null)
                {
                    var photo = _photoAccessor.AddPhoto(user.Photo);

                    var photoUpload = new Photo
                    {
                        IsMain     = !await _photoContext.FindUserImage(model.Item2),
                        PublicId   = photo.PublicId,
                        Url        = photo.Url,
                        UploadTime = DateTime.Now,
                        UserId     = model.Item2
                    };

                    await _photoContext.UploadImageDb(photoUpload);
                }
                await _photoContext.SaveChanges();
            }
            catch (Exception e)
            {
                var mail = _mailService.ErrorMessage(e.InnerException?.ToString() ?? e.Message);
                _mailService.SendMail("", mail, "error");
                _logger.LogError(e.InnerException?.ToString() ?? e.Message);
                throw;
            }

            if (model.Item1.Error != null)
            {
                return(BadRequest(new { model.Item1.Error }));
            }
            _mailService.VerifyEmail(user.Email, model.Item1.Code);
            var newModel = _mapper.Map <JwtModel, UserReturnModel>(model.Item1);

            return(Ok(newModel));
        }