public async Task <ActionResult <Machinery> > Post([FromForm] ToolDto model)
        {
            var user = await _accountRepository.GetByIdAsync(model.User);

            string path     = Path.Combine("img/customers", model.CustomerId.ToString(), "tool");
            string pathFull = _baseUrl.GetBaseUrl(path);
            var    data     = new Tool
            {
                Brand                   = model.Brand,
                CategoryId              = model.CategoryId,
                CustomerId              = model.CustomerId,
                DateOfPurchase          = model.DateOfPurchase,
                Model                   = model.Modelo,
                NameTool                = model.NameTool,
                Observations            = model.Observations,
                Serie                   = model.Serie,
                State                   = model.State,
                TechnicalSpecifications = model.TechnicalSpecifications,
                User = user,
            };

            if (model.Img != null)
            {
                string nameFile = _imgService.SaveFile(model.Img, pathFull, 1280, 720);
                if (model.PhotoPath != null)
                {
                    await _imgService.DeleteFile(pathFull, model.PhotoPath);
                }
                data.PhotoPath = nameFile;
            }
            await _genericRepository.CreateAsync(data);

            return(new CreatedAtRouteResult("GetTool", new { id = data.Id }, data));
        }
Ejemplo n.º 2
0
        public async Task <bool> Update(ToolDto dto, int id)
        {
            using (IDbConnection conn = Connection)
            {
                try
                {
                    string sqlGetTool = "select * from [Tool] where Id = @id1";
                    conn.Open();
                    var tool = await conn.QueryFirstOrDefaultAsync <Tool>(sqlGetTool, new { id1 = id });

                    tool.Title       = (dto.Title == null)? tool.Title : dto.Title;
                    tool.Description = (dto.Description == null) ? tool.Description : dto.Description;
                    tool.Create_id   = (dto.Create_id == 0) ? tool.Create_id : dto.Create_id;
                    tool.File_url    = (dto.File_url == null) ? tool.File_url : dto.File_url;
                    string sqlUpdate = @"update [Tool] set Create_id = @creator, Title = @title, Description = @desc, File_url = @url where Id = @idTool";
                    await conn.QueryAsync <Tool>(sqlUpdate, new { idTool = id, creator = tool.Create_id, title = tool.Title, desc = tool.Description, url = tool.File_url });

                    return(true);
                }
                catch
                {
                    return(false);
                }
            }
        }
        public async Task <IActionResult> Post(ToolDto tool)
        {
            var toolToAdd = _mapper.Map <Tool>(tool);

            toolToAdd.Added = DateTime.Now;

            if (tool.CurrentState != null)
            {
                toolToAdd.StateHistory = new ToolState[] { _mapper.Map <ToolState>(tool.CurrentState) };
            }
            else
            {
                toolToAdd.StateHistory = new ToolState[] {
                    new ToolState()
                    {
                        State = "new", Note = "Added to the toolkit", Tool = toolToAdd, WhenChanged = DateTime.Now
                    }
                };
            }


            var newTool = await _repo.AddTool(toolToAdd);

            var toolToReturn = _mapper.Map <ToolDto>(toolToAdd);

            return(Ok(toolToReturn));
        }
        public async Task <IActionResult> Put(int id, [FromForm] ToolDto model)
        {
            var user = await _accountRepository.GetByIdAsync(model.User);

            string path     = Path.Combine("img/customers", model.CustomerId.ToString(), "tool");
            string pathFull = _baseUrl.GetBaseUrl(path);

            var data = await _genericRepository.GetAsyncById(id);

            data.Brand                   = model.Brand;
            data.CategoryId              = model.CategoryId;
            data.CustomerId              = model.CustomerId;
            data.DateOfPurchase          = model.DateOfPurchase;
            data.Model                   = model.Modelo;
            data.NameTool                = model.NameTool;
            data.Observations            = model.Observations;
            data.Serie                   = model.Serie;
            data.State                   = model.State;
            data.TechnicalSpecifications = model.TechnicalSpecifications;
            data.User = user;

            if (model.Img != null)
            {
                string nameFile = _imgService.SaveFile(model.Img, pathFull, 1280, 720);
                if (model.PhotoPath != null)
                {
                    await _imgService.DeleteFile(pathFull, model.PhotoPath);
                }
                data.PhotoPath = nameFile;
            }

            await _genericRepository.UpdateAsync(data);

            return(NoContent());
        }
Ejemplo n.º 5
0
 public void Put(string id, [FromBody] ToolDto toolDto)
 {
     try
     {
         _toolService.UpdateTool(id, toolDto);
     }
     catch (Exception e)
     {
         _logger.LogError(e.Message, e);
         throw;
     }
 }
Ejemplo n.º 6
0
        public void AddTool(ToolDto toolDto)
        {
            if (toolDto == null)
            {
                throw new ArgumentNullException(nameof(toolDto));
            }

            var tool = _mapper.Map <Tool>(toolDto);

            _toolRepository.Add(tool);
            _toolRepository.Commit();
        }
Ejemplo n.º 7
0
 public void Post([FromBody] ToolDto toolDto)
 {
     try
     {
         _toolService.AddTool(toolDto);
     }
     catch (Exception e)
     {
         _logger.LogError(e.Message, e);
         throw;
     }
 }
        public async Task <IActionResult> Put(int id, ToolDto tool)
        {
            var toolFromRepo = await _repo.GetTool(id);

            _mapper.Map(tool, toolFromRepo);

            if (await _repo.SaveAll())
            {
                return(NoContent());
            }

            throw new Exception($"Updating tool {id} failed on save");
        }
Ejemplo n.º 9
0
        public async Task <string> Register([FromBody] ToolDto dto)
        {
            var result = await _toolService.Register(dto);

            if (result)
            {
                return("Chờ duyệt tool");
            }
            else
            {
                return("Đăng ký tool thất bại");
            }
        }
Ejemplo n.º 10
0
        public async Task <string> Update([FromBody] ToolDto dto, int id)
        {
            var result = await _toolService.Update(dto, id);

            if (result)
            {
                return("Cập nhật thành công");
            }
            else
            {
                return("Cập nhật thất bại");
            }
        }
Ejemplo n.º 11
0
        public IHttpActionResult CreateTool(ToolDto toolDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var tool = Mapper.Map <ToolDto, Tool>(toolDto);

            _context.Tools.Add(tool);
            _context.SaveChanges();

            toolDto.Id = tool.Id;

            return(Created(new Uri(Request.RequestUri + "/" + tool.Id), toolDto));
        }
Ejemplo n.º 12
0
 public async Task <bool> Register(ToolDto dto)
 {
     using (IDbConnection conn = Connection)
     {
         try
         {
             string sql = @"insert into [Tool]([Create_id],[Title],[Description],[File_url]) values(@creator,@title,@desc,@url)";
             conn.Open();
             conn.QueryFirstOrDefault <Tool>(sql, new { creator = dto.Create_id, title = dto.Title, desc = dto.Description, url = dto.File_url });
             return(true);
         }
         catch
         {
             return(false);
         }
     }
 }
Ejemplo n.º 13
0
        public ToolDto AddTool(ToolDto toolDto)
        {
            var tool = new ToolEntity
            {
                Name        = toolDto.Name,
                Description = toolDto.Description,
                Title       = toolDto.Title
            };

            _context.Tools.Add(tool);
            _context.SaveChanges();

            foreach (var link in toolDto.Links)
            {
                _resourceService.AddResource(tool.Id, link);
            }
            toolsDTO.Add(toolDto);
            return(toolDto);
        }
Ejemplo n.º 14
0
        public IHttpActionResult UpdateTool(int id, ToolDto toolDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var toolInDb = _context.Tools.SingleOrDefault(t => t.Id == id);

            if (toolInDb == null)
            {
                return(NotFound());
            }

            Mapper.Map(toolDto, toolInDb);

            _context.SaveChanges();

            return(Ok());
        }
Ejemplo n.º 15
0
        public async Task <ActionResult <IEnumerable <ToolDto> > > Get([FromServices] DataContext context, string tag)
        {
            if (tag != null)
            {
                var tools = await context.Tools.Where(t => t.Tags.Contains(tag)).ToListAsync();

                List <ToolDto> toolsDto = new List <ToolDto>();
                foreach (var item in tools)
                {
                    var toolDto = new ToolDto()
                    {
                        Description = item.Description,
                        Id          = item.Id,
                        Title       = item.Title,
                        Link        = item.Link,
                        Tags        = item.Tags.Split(",").ToList()
                    };
                    toolsDto.Add(toolDto);
                }
                return(toolsDto);
            }
            else
            {
                var tools = await context.Tools.ToListAsync();

                List <ToolDto> toolsDto = new List <ToolDto>();
                foreach (var item in tools)
                {
                    var toolDto = new ToolDto()
                    {
                        Description = item.Description,
                        Id          = item.Id,
                        Title       = item.Title,
                        Link        = item.Link,
                        Tags        = item.Tags.Split(",").ToList()
                    };
                    toolsDto.Add(toolDto);
                }
                return(toolsDto);
            }
        }
Ejemplo n.º 16
0
        public void UpdateTool(string id, ToolDto toolDto)
        {
            if (toolDto == null)
            {
                throw new ArgumentNullException(nameof(toolDto));
            }

            var tool = _toolRepository.GetById(id);

            //to update the primary key entity should be removed to insert a new one
            if (id != toolDto.SerialNumber)
            {
                _toolRepository.Remove(tool);
                AddTool(toolDto);
            }
            else
            {
                _mapper.Map(toolDto, tool);
                _toolRepository.Commit();
            }
        }
Ejemplo n.º 17
0
 public IActionResult Add(ToolDto toolDto)
 {
     return(Ok(_toolService.AddTool(toolDto)));
 }
Ejemplo n.º 18
0
        public async Task <ActionResult <Tool> > Post([FromServices] DataContext context, ToolDto toolDto)
        {
            Tool tool = new Tool()
            {
                Description = toolDto.Description,
                Link        = toolDto.Link,
                Title       = toolDto.Title,
                Tags        = string.Join(",", toolDto.Tags)
            };
            var tos = tool;

            if (ModelState.IsValid)
            {
                await context.Tools.AddAsync(tool);

                await context.SaveChangesAsync();

                return(CreatedAtAction(nameof(Post), tool));
            }
            else
            {
                return(BadRequest(ModelState));
            }
        }
Ejemplo n.º 19
0
 public IActionResult Update(ToolDto toolDto)
 {
     throw new NotImplementedException();
 }