Esempio 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));
        }
Esempio n. 2
0
        public static async Task <bool> UploadPost(this PostModel post, string userId, IPhoto photoUploads,
                                                   IPhotoAccessor photoAccessor, IYoutube youtubeuploader, IVideo videoContext, IPost postContext, Post model)
        {
            model.UserId = userId;
            var postId = Guid.NewGuid();

            model.Id         = postId;
            model.DatePosted = DateTime.Now;
            ;
            if (post.Photo != null)
            {
                var photoUpload = photoAccessor.AddPhoto(post.Photo);
                var photo       = new Photo
                {
                    PostId     = postId,
                    PublicId   = photoUpload.PublicId,
                    UploadTime = DateTime.Now,
                    Url        = photoUpload.Url,
                    UserId     = userId
                };


                await photoUploads.UploadImageDb(photo);
            }

            if (post.Video != null)
            {
                var video = new Videos();

                UploadViewModel upload = new UploadViewModel();
                upload.Description = post.Video.Name;
                upload.Type        = post.Video.ContentType;
                upload.CategoryId  = String.Empty;
                upload.Title       = post.Video.FileName;
                upload.VideoTags   = new string[] { "tag1", "tag2" };
                upload.Private     = false;
                var videoUpload = await youtubeuploader.UploadVideo(upload, post.Video);

                if (!string.IsNullOrEmpty(videoUpload.VideoId))
                {
                    video.DateUploaded = DateTime.Now;
                    video.UserId       = userId;
                    video.PublicId     = videoUpload.VideoId;
                    video.PostId       = model.Id;
                    await videoContext.Post(video);
                }
            }

            await postContext.Post(model);

            return(await videoContext.SaveChanges());
        }