public async Task <IActionResult> Create([FromBody] CreateDeviceRequest request) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { var itemId = await _devicesRepository.Create(request); return(CreatedAtAction(nameof(GetById), new { id = itemId }, itemId)); } catch (InvalidOfficeException) { string errorText = String.Format("Office with ID: {0} doesn't exist", request.OfficeId); return(StatusCode(StatusCodes.Status409Conflict, new { Message = errorText })); } catch (InvalidBrandException) { string errorText = String.Format("Brand with ID: {0} doesn't exist", request.BrandId); return(StatusCode(StatusCodes.Status409Conflict, new { Message = errorText })); } catch (InvalidModelException) { string errorText = String.Format("Model with ID: {0} doesn't exist", request.ModelId); return(StatusCode(StatusCodes.Status409Conflict, new { Message = errorText })); } catch (DuplicateDeviceSerialNumberException) { string errorText = String.Format("Device with Serial number: {0} already exist", request.SerialNum); return(StatusCode(StatusCodes.Status409Conflict, new { Message = errorText })); } catch (DuplicateModelException) { string errorText = String.Format("Model with name: {0} already exist", request.ModelName); return(StatusCode(StatusCodes.Status409Conflict, new { Message = errorText })); } catch (DuplicateDeviceIdentificationNumberException) { string errorText = String.Format("Device with identification number: {0} already exist", request.IdentificationNum); return(StatusCode(StatusCodes.Status409Conflict, new { Message = errorText })); } }
public ActionResult Add(string deviceName, double memoryValue, string userName) { User tempUser = _usersRepository.Get(userName); foreach (Device device in _devicesRepository.Get()) { if (device.Name == deviceName) { return(View("~/Views/Admin/SameNameDevice.cshtml")); } } Device tempDevice = new Device() { Name = deviceName, Memory = memoryValue, User = tempUser }; _devicesRepository.Create(tempDevice); tempUser.Devices.Add(tempDevice); return(Redirect("/Admin/Index")); }
public async Task <EntityState> Create(Device device) { return(await _devicesRepository.Create(device)); }