[ActionName("AddCpu")] //[FromBody]CPU cpu,[FromUri]string token
        public IHttpActionResult Post(CPU cpu)
        {
            if (cpu.Name == null)
                return BadRequest("Нет данных");

            var header = Request.Headers;
            if (header == null)
                return BadRequest("Нет заголовка запроса");

            var cookie = Request.Headers.GetCookies("session").FirstOrDefault();
            if (cookie == null) return Unauthorized();

            var token = cookie["token"].Value;

            switch (_tokenCheck.AuthRequest(token))
            {
                case AuthReturnState.SessionIsNull:
                    return InternalServerError();
                case AuthReturnState.ExpiresOut:
                    return BadRequest("Токен просрочен");
                case AuthReturnState.AuthWrog:
                    return ResponseMessage(new HttpResponseMessage(HttpStatusCode.Forbidden));
                case AuthReturnState.TokenFalse:
                    return Unauthorized();
                case AuthReturnState.AccessGranted:
                    _cpuRepository.AddCpu(cpu);
                    return Ok(cpu);
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }
Example #2
0
        [ActionName("AddCpu")] //[FromBody]CPU cpu,[FromUri]string token
        public IHttpActionResult Post(CPU cpu)
        {

            var header = Request.Headers;
            
            if (header == null)
                return BadRequest("Нет заголовка запроса");

            switch (_sessionHelper.AuthRequest(header.Authorization.ToString()))
            {
                case AuthReturnState.SessionIsNull:
                    return InternalServerError();
                case AuthReturnState.ExpiresOut:
                    return BadRequest("Токен просрочен");
                case AuthReturnState.AuthWrog:
                    return ResponseMessage(new HttpResponseMessage(HttpStatusCode.Forbidden));
                case AuthReturnState.TokenFalse:
                    return Unauthorized();
                case AuthReturnState.AccessGranted:
                    _cpuRepository.AddCpu(cpu);
                    return Ok(cpu);
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }
 public void AddCpu(CPU cpu)
 {
     if (cpu.CPUId == 0)
     {
         _context.Cpus.Add(cpu);
     }
     _context.SaveChanges();
 }
 public void UpdateCpu(int id, CPU cpu)
 {
     var dbEntry = _context.Cpus.Find(id);
     if (dbEntry != null)
     {
         dbEntry.Name = cpu.Name;
         dbEntry.Description = cpu.Description;
         dbEntry.Price = cpu.Price;
         dbEntry.Manufacturer = cpu.Manufacturer;
         dbEntry.ManufacturerId = cpu.ManufacturerId;
     }
     _context.SaveChanges();
 }
        public void PatchCpu( int id, CPU cpu)
        {
            var dbEntry = _context.Cpus.Find(id);
            if (dbEntry != null)
            {
                dbEntry.Name = string.IsNullOrEmpty(cpu.Name) ? dbEntry.Name : cpu.Name;
                dbEntry.Description = string.IsNullOrEmpty(cpu.Description) ? dbEntry.Description : cpu.Description;

                if (cpu.Price != 0)
                    dbEntry.Price = cpu.Price;
                
                dbEntry.Manufacturer = string.IsNullOrEmpty(cpu.Manufacturer) ? dbEntry.Manufacturer : cpu.Manufacturer;

                if (dbEntry.ManufacturerId != 0)
                    dbEntry.ManufacturerId = cpu.ManufacturerId;
            }
            _context.SaveChanges();
        }