Example #1
0
        public dynamic Putstaff(long id, [FromBody] StaffRequestDto staffDto)
        {
            String authen_token = null;

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != staffDto.id)
            {
                return(BadRequest());
            }

            try
            {
                authen_token = staffRepository.updateStaff(id, staffDto);
            }
            catch (Exception e)
            {
                if (!staffRepository.staffExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw e;
                }
            }

            return(Ok(authen_token));
        }
Example #2
0
 public async Task <IActionResult> UpdateStaff(StaffRequestDto staffRequestDto)
 {
     return(Ok(await _mediator.Send(new UpdateStaffCommand
     {
         StaffRequestDto = staffRequestDto
     })));
 }
        internal String updateStaff(long id, StaffRequestDto staffDto)
        {
            String tokenString = null;
            Staff  staff       = db.Staff.Where(s => s.Id == id).FirstOrDefault();

            if (staff != null)
            {
                staff.Name       = staffDto.name;
                staff.Gmail      = staffDto.gmail;
                staff.PositionId = staffDto.positionId;
                staff.StoreId    = staffDto.storeId;
                staff.StatusId   = staffDto.statusId;
                staff.PicUrl     = staffDto.picUrl;
                var          tokenHandler = new JwtSecurityTokenHandler();
                var          key          = Encoding.Default.GetBytes(SECRETKEY);
                var          claim        = new Claim(ClaimTypes.NameIdentifier, staff.Id.ToString());
                var          claim2       = new Claim(ClaimTypes.Name, staff.Name);
                var          claim3       = new Claim(ClaimTypes.Role, staff.PositionId);
                List <Claim> claims       = new List <Claim>()
                {
                    claim, claim2, claim3
                };

                var tokenDescriptor = new SecurityTokenDescriptor
                {
                    Issuer             = "SWH",
                    Audience           = "SWH",
                    Subject            = new ClaimsIdentity(claims),
                    Expires            = DateTime.UtcNow.AddDays(30),
                    SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
                };

                var token = tokenHandler.CreateToken(tokenDescriptor);
                tokenString = tokenHandler.WriteToken(token);

                staff.AuthToken = tokenString;
            }

            db.Entry(staff).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            db.SaveChanges();
            return(tokenString);
        }
        public IHttpActionResult Putstaff(string accessToken, [FromBody] StaffRequestDto staffDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (accessToken != staffDto.accessToken)
            {
                return(BadRequest());
            }

            staff staff = db.staffs.Where(s => s.auth_token == accessToken).FirstOrDefault();

            staff.name        = staffDto.name;
            staff.gmail       = staffDto.gmail;
            staff.position_id = staffDto.positionId;
            staff.store_id    = staffDto.storeId;
            staff.status_id   = staffDto.statusId;
            staff.pic_url     = staffDto.picUrl;


            db.Entry(staff).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!staffExists(accessToken))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
        internal StaffRequestDto addStaff(StaffRequestDto staffDto)
        {
            Staff staff = new Staff();

            staff.Id         = staffDto.id;
            staff.Name       = staffDto.name;
            staff.Gmail      = staffDto.gmail;
            staff.PositionId = staffDto.positionId;
            staff.StoreId    = staffDto.storeId;
            staff.StatusId   = staffDto.statusId;
            staff.PicUrl     = staffDto.picUrl;

            // set username password trong db
            var          tokenHandler = new JwtSecurityTokenHandler();
            var          key          = Encoding.Default.GetBytes(SECRETKEY);
            var          claim        = new Claim(ClaimTypes.NameIdentifier, staff.Id.ToString());
            var          claim2       = new Claim(ClaimTypes.Name, staff.Name);
            var          claim3       = new Claim(ClaimTypes.Role, staff.PositionId);
            List <Claim> claims       = new List <Claim>()
            {
                claim, claim2, claim3
            };

            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Issuer             = "SWH",
                Audience           = "SWH",
                Subject            = new ClaimsIdentity(claims),
                Expires            = DateTime.UtcNow.AddDays(30),
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
            };

            var token       = tokenHandler.CreateToken(tokenDescriptor);
            var tokenString = tokenHandler.WriteToken(token);

            staff.AuthToken    = tokenString;
            staffDto.authToken = tokenString;
            db.Staff.Add(staff);
            db.SaveChanges();
            return(staffDto);
        }
        public IHttpActionResult Poststaff(StaffRequestDto staffDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            staff staff = new staff();

            staff.name        = staffDto.name;
            staff.gmail       = staffDto.gmail;
            staff.position_id = staffDto.positionId;
            staff.store_id    = staffDto.storeId;
            staff.status_id   = staffDto.statusId;
            staff.pic_url     = staffDto.picUrl;
            staff.auth_token  = staffDto.accessToken;

            db.staffs.Add(staff);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateException)
            {
                if (staffExists(staffDto.accessToken))
                {
                    return(Conflict());
                }
                else
                {
                    throw;
                }
            }

            return(Ok(staffDto));
        }
Example #7
0
        public dynamic Poststaff([FromBody] StaffRequestDto staffDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            try
            {
                staffDto = staffRepository.addStaff(staffDto);
            }
            catch (Exception e)
            {
                if (staffRepository.staffExists(staffDto.id))
                {
                    return(Conflict());
                }
                else
                {
                    throw e;
                }
            }

            return(Ok(staffDto));
        }