Пример #1
0
        public async Task <ActionResult <Employee> > PostEmployee(Employee employee)
        {
            _contextDb.Employees.Add(employee);
            await _contextDb.SaveChangesAsync();

            return(CreatedAtAction(nameof(GetEmployee), new { id = employee.ID }, employee));
        }
Пример #2
0
        public async Task <ActionResult <User> > PostUser(User user)
        {
            if (_contextDb.Users.Where(x => x.Username == user.Username).FirstOrDefault() != null) //patikrina ar nera jau tokio email
            {
                return(ValidationProblem());
            }
            _contextDb.Users.Add(user);
            await _contextDb.SaveChangesAsync();

            return(Ok("user created"));
        }
Пример #3
0
        public async Task <IActionResult> PutClient(int id, Client client)
        {
            var    identity = HttpContext.User.Identity as ClaimsIdentity;
            string sid;

            if (identity != null)
            {
                sid = identity.FindFirst("id").Value;
            }
            else
            {
                return(ValidationProblem());
            }
            Client klientukas = await _contextDb.Clients.Where(j => j.ID == id).FirstOrDefaultAsync();

            if (klientukas == null)
            {
                return(BadRequest("Couldnt find matching ID of client"));
            }
            if (identity.FindFirst(ClaimTypes.Role).Value == "Administrator" ||
                int.Parse(sid) == klientukas.fk_User)   // gali redaguoti tik administratorius ir pats klientas
            {
                if (klientukas != null)
                {
                    _contextDb.Entry(klientukas).State = EntityState.Detached;
                }

                client.ID      = id;
                client.fk_User = klientukas.fk_User;

                _contextDb.Entry(client).State = EntityState.Modified;
                await _contextDb.SaveChangesAsync();

                return(Ok(client));
            }
            else
            {
                return(ValidationProblem());
            }
        }
Пример #4
0
        public async Task <ActionResult <Employee> > PostOrder(Order order) //be fk_client ir ID
        {
            var    identity = HttpContext.User.Identity as ClaimsIdentity;
            string sid;

            if (identity != null)
            {
                sid = identity.FindFirst("id").Value;
            }
            else
            {
                return(ValidationProblem());
            }

            Client clientFromClaim = await _contextDb.Clients.Where(j => j.fk_User == int.Parse(sid)).FirstOrDefaultAsync();

            if (clientFromClaim == null)//patikrina ar klientas egzistuoja
            {
                return(ValidationProblem());
            }
            else
            {
                order.fk_Client = clientFromClaim.ID;
            }

            Employee employee = await _contextDb.Employees.Where(j => j.ID == order.fk_Employee).FirstOrDefaultAsync();

            if (employee == null)//patikrina ar darbuotojas gali buti priskirtas uzsakymui
            {
                return(ValidationProblem());
            }

            _contextDb.Orders.Add(order);
            await _contextDb.SaveChangesAsync();

            return(CreatedAtAction(nameof(GetOrder), new { id = order.ID }, order));
        }