예제 #1
0
        public async Task <bool> AddReader(Reader newReader)
        {
            await _library.Reader.AddAsync(newReader);

            var affectedRows = await _library.SaveChangesAsync();

            return(affectedRows > 0);
        }
예제 #2
0
        public async Task <IActionResult> Post([FromBody] Register credentials) //TODO: Create Exceptions
        {
            if (!credentials.AreValid())
            {
                return(BadRequest("Invalid Credentials"));
            }
            var passwordHash = BCrypt.Net.BCrypt.HashPassword(credentials.Password);
            var reader       = new Reader()
            {
                Email     = credentials.Email,
                Rtype     = credentials.Type,
                Firstname = credentials.Firstname,
                Lastname  = credentials.Lastname,
                Address   = credentials.Address,
                Zipcode   = credentials.Zipcode,
            };

            var readerAdded = await _repository.AddReader(reader);

            if (!readerAdded)
            {
                return(BadRequest("Invalid Credentials"));
            }

            var user = new Users()
            {
                Readerid  = reader.Readerid,
                Username  = credentials.Username,
                Password  = passwordHash,
                Email     = credentials.Email,
                Firstname = credentials.Firstname,
                Lastname  = credentials.Lastname,
                Role      = credentials.Role
            };

            var userAdded = await _repository.AddUser(user);

            if (!userAdded)
            {
                return(BadRequest("Invalid Credentials"));
            }

            return(Ok(new
            {
                Username = user.Username,
                Readerid = reader.Readerid,
                Email = reader.Email,
                Firstname = reader.Firstname,
                Lastname = reader.Lastname,
                Address = reader.Address,
                Zipcode = reader.Zipcode,
            }));
        }