Пример #1
0
        public async Task <bool> IsReceiptAlreadyAddedAsync([NotNull] ReceiptRequestDto requestDto)
        {
            if (requestDto == null)
            {
                throw new ArgumentNullException(nameof(requestDto));
            }
            var hash = _HashCalculator.Calculate(requestDto);

            var isExists = await _ReceiptsRepository.IsReceiptExistsByHashAsync(hash);

            return(isExists);
        }
        public ActionResult Register(RegisterViewModel model)
        {
            string avatar = "";

            if (Request.Files.Count > 0)
            {
                string folderPath = Server.MapPath(WebConfigurationManager.AppSettings["UsersAvatars"]);
                string fileName   = _hashCalculator.Calculate(Request.Files[0].FileName);

                char[] charInvalidFileChars = Path.GetInvalidFileNameChars();
                foreach (char charInvalid in charInvalidFileChars)
                {
                    fileName = fileName.Replace(charInvalid, ' ');
                }

                string ext = Path.GetExtension(Request.Files[0].FileName);

                if (!Directory.Exists(folderPath))
                {
                    Directory.CreateDirectory(folderPath);
                }
                string filePath = Path.Combine(folderPath, fileName + ext);
                Request.Files[0].SaveAs(filePath);
                avatar = fileName;
            }

            if (ModelState.IsValid && _securityHelper.RegisterUser(model.Login, model.Password, model.Email, model.Name, model.Surname, avatar))
            {
                return(RedirectToAction("Index", "Home"));
            }
            else
            {
                return(View(model));
            }
        }
        public bool RegisterUser(string login, string password, string email, string name, string surname, string avatar)
        {
            if (String.IsNullOrEmpty(login))
            {
                throw new ArgumentNullException("login");
            }
            if (String.IsNullOrEmpty(password))
            {
                throw new ArgumentNullException("password");
            }
            if (String.IsNullOrEmpty(email))
            {
                throw new ArgumentNullException("email");
            }
            if (String.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }
            if (String.IsNullOrEmpty(surname))
            {
                throw new ArgumentNullException("surname");
            }
            if (String.IsNullOrEmpty(avatar))
            {
                avatar = "/Resourse/Images/AdminAvatar.jpg";
            }

            using (DBEntities db = new DBEntities())
            {
                if (db.Users.FirstOrDefault(user => user.Login.Equals(login)) != null)
                {
                    return(false);
                }

                //TODO: check id_group, check avatarpath for user adding.

                return(db.Users.Add(new User
                {
                    Name = name,
                    Surname = surname,
                    Email = email,
                    Login = login,
                    Password = _hashCalculator.Calculate(password),
                    AvatarPath = avatar,
                    DateOfRegistration = DateTime.Now.ToString(),
                    IsActive = false,
                    Id_Group = 2
                }));
            }
        }
        public HttpResponseMessage Post()
        {
            HttpResponseMessage result = null;
            var httpRequest            = HttpContext.Current.Request;

            if (httpRequest.Files.Count > 0)
            {
                var files = new List <string>();

                foreach (string file in httpRequest.Files)
                {
                    var    postedFile           = httpRequest.Files[file];
                    string fileName             = _hashCalculator.Calculate(postedFile.FileName);
                    char[] charInvalidFileChars = Path.GetInvalidFileNameChars();
                    foreach (char charInvalid in charInvalidFileChars)
                    {
                        fileName = fileName.Replace(charInvalid, ' ');
                    }
                    string ext             = Path.GetExtension(postedFile.FileName);
                    string folder          = WebConfigurationManager.AppSettings["NotesForModeration"];
                    string folderDirectory = Path.Combine(HttpContext.Current.Server.MapPath(folder));

                    if (!Directory.Exists(folderDirectory))
                    {
                        Directory.CreateDirectory(folderDirectory);
                    }

                    var filePath = Path.Combine(folderDirectory, fileName + ext);
                    postedFile.SaveAs(filePath);
                    files.Add(filePath);
                }
                result = Request.CreateResponse(HttpStatusCode.Created, files);
            }
            else
            {
                result = Request.CreateResponse(HttpStatusCode.BadRequest);
            }
            return(result);
        }