public IActionResult CreateWorkplacement([FromBody] RequestWorkplacement requestWorkplacement)
        {
            //check if request's body is valid
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            int userId = Convert.ToInt32(User.Claims.FirstOrDefault(c => c.Type == JwtRegisteredClaimNames.Jti).Value);

            Workplacement newWorkplacement = new Workplacement
            {
                Title       = requestWorkplacement.Title,
                Description = requestWorkplacement.Description,
                OwnerId     = userId
            };

            _context.Workplacements.Add(newWorkplacement);
            _context.SaveChanges();

            //Add user to workplacement
            UserWorkplacement userWorkplacement = new UserWorkplacement
            {
                UserId          = userId,
                WorkplacementId = newWorkplacement.WorkplacementId,
                CanManageTasks  = true
            };

            _context.UserWorkplacements.Add(userWorkplacement);
            _context.SaveChanges();

            return(Ok(new { message = "Nowe miejsce pracy zostało pomyślnie zarejestrowane! ", id = newWorkplacement.WorkplacementId }));
        }
Beispiel #2
0
        public IActionResult RegisterUser([FromBody] RequestUser requestUser)
        {
            //Check if model is valid
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            //Check if username is avaliable
            var userWithProvidedUsername = _context
                                           .Users
                                           .SingleOrDefault(u => u.Username == requestUser.Username);

            if (userWithProvidedUsername != null)
            {
                return(Ok(new { error = "Ta nazwa użytkownika jest już zajęta!" }));
            }

            User newUser = new User()
            {
                Username     = requestUser.Username,
                Name         = requestUser.Name,
                Surname      = requestUser.Surname,
                Email        = requestUser.Email,
                PasswordHash = BCrypt.Net.BCrypt.HashPassword(requestUser.Password)
            };

            _context.Users.Add(newUser);
            _context.SaveChanges();

            return(Ok(new { message = "Pomyślnie zarejestrowano!" }));
        }
Beispiel #3
0
        public IActionResult CreateTask([FromBody] RequestTask requestTask)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            if (requestTask.Priority != "highest" && requestTask.Priority != "high" && requestTask.Priority != "normal" && requestTask.Priority != "low")
            {
                return(BadRequest());
            }

            int userId = Convert.ToInt32(User.Claims.FirstOrDefault(c => c.Type == JwtRegisteredClaimNames.Jti).Value);

            var workplacement = _context.Workplacements.SingleOrDefault(w => w.WorkplacementId == requestTask.WorkplacementId);

            if (workplacement == null)
            {
                return(NotFound());
            }

            var employee = _context.Users.SingleOrDefault(e => e.UserId == requestTask.EmployeeId);

            if (employee == null && requestTask.EmployeeId != -1)
            {
                return(NotFound());
            }

            var addedBy = _context.Users.SingleOrDefault(a => a.UserId == requestTask.AddedById);

            if (addedBy == null)
            {
                return(NotFound());
            }

            // Now check if can manage tasks
            var canManageTasks = _context.UserWorkplacements.Single(uw => uw.UserId == userId && uw.WorkplacementId == workplacement.WorkplacementId).CanManageTasks;

            if (!canManageTasks)
            {
                return(Unauthorized());
            }

            int?employeeId = requestTask.EmployeeId;

            if (employeeId == -1)
            {
                employeeId = null;
            }

            Models.Task newTask = new Models.Task
            {
                WorkplacementId = requestTask.WorkplacementId,
                EmployeeId      = employeeId,
                AddedById       = requestTask.AddedById,
                Title           = requestTask.Title,
                Description     = requestTask.Description,
                Priority        = requestTask.Priority,
                AddDate         = DateTime.Now,
                Status          = "todo"
            };
            _context.Tasks.Add(newTask);
            _context.SaveChanges();

            return(Ok(new { message = "Nowe zadanie zostało pomyślnie utworzone!" }));
        }