Exemplo n.º 1
0
        public async Task <bool> CreateEquipmentAsync(int laboratoryId, CreateEquipmentRequest request)
        {
            if (request.Name.Length == 0)
            {
                throw new RequestError("Name can not be empty");
            }
            if (request.Name.Length > 200)
            {
                throw new RequestError("Name can not be longer than 200");
            }
            if (request.CatalogName != null && request.CatalogName.Length > 200)
            {
                throw new RequestError("Catalog name can not be longer than 200");
            }
            if (request.Manufacturer != null && request.Manufacturer.Length > 200)
            {
                throw new RequestError("Manufacturer name can not be longer than 200");
            }
            if (request.Datasheet != null && request.Datasheet.Length > 300)
            {
                throw new RequestError("Datasheet can not be longer than 300");
            }
            if (request.Year > DateTime.Now.Year)
            {
                throw new RequestError("Invalid year of produciton");
            }
            Laboratory laboratory = await unitOfWork.Laboratories.Where(l => l.Id == laboratoryId).SingleOrDefaultAsync();

            if (laboratory == default(Laboratory))
            {
                throw new RequestError(404, "Non existing laboratory");
            }

            // AUTHORISATION CHECK
            int myId = authService.CurrentUser.Id;

            if (myId != laboratory.Coordinator.Id &&
                laboratory.Permissions.Where(p => p.UserId == myId).SingleOrDefault() == default(LaboratoryPermission))
            {
                throw new RequestError(403, "Permission denied!");
            }

            Equipment equipment = new Equipment
            {
                Laboratory   = laboratory,
                Name         = request.Name,
                Year         = request.Year,
                CatalogName  = request.CatalogName,
                Datasheet    = request.Datasheet,
                ImageLink    = request.ImageLink,
                Manufacturer = request.Manufacturer,
                Description  = request.Description,
            };

            laboratory.Equipment.Add(equipment);
            await unitOfWork.SaveAsync();

            return(true);
        }
        public async Task <ActionResult <CreateEquipmentResponse> > CreateEquipment(CreateEquipmentRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!EquipmentStatus.IsEquipmentStatus(request.Status))
            {
                return(BadRequest("Status is not valid. Must be: " + string.Join(", ", EquipmentStatus.ListAllStatuses())));
            }

            return(await _equipmentService.CreateEquipment(request));
        }
 public ActionResult Create([FromBody] CreateEquipmentRequest request)
 {
     if (!ModelState.IsValid)
     {
         return(BadRequest());
     }
     try
     {
         _equipmentService.Create(request.Id, request.Name, request.SerialNumber);
         return(NoContent());
     }
     catch (Exception error)
     {
         _logger.LogError(error.ToString());
         return(BadRequest());
     }
 }
Exemplo n.º 4
0
 public async Task <bool> CreateEquipmentAsync([FromRoute] int id, [FromBody] CreateEquipmentRequest request)
 {
     return(await laboratoryService.CreateEquipmentAsync(id, request));
 }