Example #1
0
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            _enclosureDao = (EnclosureDao)context.ActionArguments["enclosure"];

            using IDbConnection database = new SqlConnection(MyConnectionString);
            const string sql     = "SELECT * FROM Electric.Project WHERE id = @projectId";
            var          project = database.QueryFirstOrDefault <ProjectDao>(sql, new { projectId = _enclosureDao.ProjectId });

            if (project == null)
            {
                throw new ProjectNotFountException("Project does not exist!");
            }

            base.OnActionExecuting(context);
        }
Example #2
0
        private Models.Enclosure TransformDaoToBusinessLogicEnclosure(EnclosureDao enclosureDao)
        {
            var devices   = _device.GetDevicesForEnclosure(enclosureDao.Id);
            var enclosure = new Models.Enclosure()
            {
                Id             = enclosureDao.Id,
                Name           = enclosureDao.Name,
                Date           = enclosureDao.Date,
                ProjectId      = enclosureDao.ProjectId,
                Devices        = devices,
                TotalPrice     = enclosureDao.TotalPrice,
                EnclosureSpecs = _enclosureSpecs.GetEnclosureSpecsByEnclosureId(enclosureDao.Id),
            };

            return(enclosure);
        }
Example #3
0
 public ActionResult <Models.Enclosure> CreateEnclosure([FromBody] EnclosureDao enclosure)
 {
     try
     {
         return(Created("https://localhost:5001/Enclosure", _enclosure.CreateEnclosure(enclosure)));
     }
     catch (ProjectNotFountException ex)
     {
         return(NotFound(ex.Message));
     }
     catch (Exception ex)
     {
         _logger.LogError("Error", ex);
         return(BadRequest());
     }
 }
Example #4
0
        public Models.Enclosure CreateEnclosure(EnclosureDao enclosure)
        {
            // if (!DoesProjectExist(enclosure.ProjectId))
            // {
            //     throw new ProjectNotFountException("Project does not exist!");
            // }

            var enclosureDao = new EnclosureDao()
            {
                Name      = enclosure.Name,
                Date      = DateTime.Now,
                ProjectId = enclosure.ProjectId,
            };

            const string insertQuery = "INSERT INTO Electric.Enclosure VALUES (@name, @date, @projectId, null) SELECT * FROM Electric.Enclosure WHERE id = SCOPE_IDENTITY()";

            return(TransformDaoToBusinessLogicEnclosure(_database.QueryFirst <EnclosureDao>(insertQuery, enclosureDao)));
        }