Пример #1
0
        public async Task <OperationResult> Create(UserDto userDto)
        {
            if (_unitOfWork.UserRepo.Get().Any(u => u.Email == userDto.Email))
            {
                return(new OperationResult(false, "Email already exists in database", "Email"));
            }
            if (_unitOfWork.UserRepo.Get().Any(u => u.Nickname == userDto.Nickname))
            {
                return(new OperationResult(false, "Nickname already exists", "Nickname"));
            }
            var user = _mapper.Map <User>(userDto);

            user.Role = _unitOfWork.RoleRepo.Get().FirstOrDefault(r => r.Name == "User");


            var result = _unitOfWork.UserRepo.Insert(user);

            if (result.Email != user.Email || result.Id == Guid.Empty)
            {
                return(new OperationResult(false, "Registration failed", ""));
            }

            await _unitOfWork.SaveAsync();

            userDto.Id = result.Id;
            if (!userDto.EmailConfirmed)
            {
                await _mediator.Publish(new RegisterVerificationMessage(userDto));
            }

            return(new OperationResult(true, "Registration success", ""));
        }
Пример #2
0
        public async Task <ActionResult> Create(HttpPostedFileBase file, PostViewModel post)
        {
            try
            {
                // TODO: Add insert logic here
                if (file != null && file.ContentLength > 0)
                {
                    var fileName   = Path.GetFileName(file.FileName);
                    int idx        = fileName.LastIndexOf('.');
                    var extensions = fileName.Substring(idx + 1);
                    if (!ImageService.IsImage(extensions))
                    {
                        return(RedirectToAction("Create"));
                    }

                    var path       = Path.Combine(Server.MapPath("~/Content/Images/Posts"), fileName);
                    var serverPath = ImageService.ImagePostPathServer(fileName);


                    var listTags = new List <Tag>();
                    var tags     = post.Options.Split(',');

                    foreach (var tag in tags)
                    {
                        listTags.Add(_Repostiory.TagRepository.GetTagOrAdd(tag));
                    }

                    if (post != null)
                    {
                        post.Id        = Guid.NewGuid().ToString();
                        post.PublishAt = DateTime.Now;
                        _Repostiory.PostRepository.AddPost(new Post
                        {
                            Title         = post.Title,
                            AllowComments = post.AllowComments,
                            Author        = User.Identity.Name,
                            Content       = post.Content,
                            Description   = post.Description,
                            Id            = Guid.Parse(post.Id),
                            ImageUrl      = serverPath,
                            PublishAt     = post.PublishAt,
                            Tags          = listTags,
                        });

                        await _Repostiory.SaveAsync();

                        file.SaveAs(path);
                    }
                }

                return(RedirectToAction("Index"));
            }

            catch (Exception e)
            {
                Console.WriteLine(e);
                return(View());
            }
        }
Пример #3
0
        public OperationResult Authenticate(string email, string password)
        {
            var user = _userService.GetByEmailOrNickname(email);

            if (user == null)
            {
                return(new OperationResult(false, "Invalid Login or Password", "email"));
            }

            if (user.IsBlocked)
            {
                return(new OperationResult(false, $"{email}, your account was blocked.", "email"));
            }

            if (!user.EmailConfirmed)
            {
                return(new OperationResult(false, $"{email} is not confirmed, please confirm", ""));
            }

            if (!VerifyPassword(user, password))
            {
                return(new OperationResult(false, "Invalid Login or Password", "Password"));
            }

            var token = GenerateJwt(user);

            var res = _unitOfWork.UserActivity.Insert(new UserActivity()
            {
                UserId = user.Id
            });

            _unitOfWork.SaveAsync();

            return(new OperationResult(true, token, ""));
        }
Пример #4
0
        public async Task <Photo> AddPhoto(IFormFile uploadedFile)
        {
            if (!IsValidImage(uploadedFile))
            {
                throw new ArgumentException();
            }

            byte[] imgData;
            using (var reader = new BinaryReader(uploadedFile.OpenReadStream()))
            {
                imgData = reader.ReadBytes((int)uploadedFile.Length);
            }

            var photo = new Photo
            {
                Thumb = Resize(imgData, _widthOptions.Value.Thumbnail),
                Img   = Resize(imgData, _widthOptions.Value.Image),
            };

            _unitOfWork.PhotoRepo.Insert(photo);
            await _unitOfWork.SaveAsync();

            return(photo);
        }
Пример #5
0
        public async Task <OperationResult> CreateOrUpdate(TankDto model)
        {
            if (model.Id != null)
            {
            }

            Photo icon = await _photoService.AddPhoto(model.Photo);

            var tank = _unitOfWork.TankRepo.Insert(new Tank()
            {
                Icon = icon
            });

            await _unitOfWork.SaveAsync();

            return(new OperationResult(true, "", ""));
        }
Пример #6
0
        public async Task <OperationResult> CreateOrUpdate(MapDto model)
        {
            if (model.Id != null)
            {
            }

            Photo icon = await _photoService.AddPhoto(model.Photo);

            if (model.Preview != null)
            {
                Photo previewIcon = await _photoService.AddPhoto(model.Preview);
            }

            var map = _unitOfWork.MapRepo.Insert(new Map()
            {
                Coordinates = model.Coordinates, WallIcon = icon
            });
            await _unitOfWork.SaveAsync();

            return(new OperationResult(true, "Ok", map.Id + " " + map.Coordinates));
        }