public async Task <IActionResult> Upsert([FromBody] UpsertArchiveCmd request) { try { var result = await _service.UpsertArchive(request); return(Ok(result)); } catch (BusinessLogicException ex) { return(BadRequest(new Response { Status = false, Message = ex.Message })); } catch (Exception e) { return(BadRequest(new Response { Status = false, Message = ErrorMessages.UnkownError })); } }
public async Task <Response> UpsertArchive(UpsertArchiveCmd request) { { /////////////////////////////////////// //var technicalCode = await _context.Equipment.AnyAsync(x => x.TechnicalCode == request.TechnicalCode); //if (technicalCode) //{ // throw new BusinessLogicException("کد فنی قبلا استفاده شده است "); //} //var amval = await _context.Equipment.AnyAsync(x => x.Amval == request.Amval); //if (amval) //{ // throw new BusinessLogicException("کداموال قبلا استفاده شده است "); //} ////////////////////////////////////////// var category = await _context.Categories.SingleOrDefaultAsync(x => x.Id == request.CategoryId); var tag = await _context.Tags.SingleOrDefaultAsync(x => x.Id == request.TageId); ///updat insert if (!string.IsNullOrEmpty(request.Id)) { var item = await _context.Archives.SingleOrDefaultAsync(x => x.Id == request.Id); if (item == null) { throw new BusinessLogicException("رکوردی یافت نشد"); } item = _mapper.Map <Archive>(request); if (category != null) { item.Category = category; } if (tag != null) { item.Tag = tag; } _context.Archives.Update(item); } else { var item = new Archive(); item = _mapper.Map <Archive>(request); if (category != null) { item.Category = category; } if (tag != null) { item.Tag = tag; } item.Id = Guid.NewGuid().ToString(); item.CreatedAt = DateTime.Now; await _context.Archives.AddAsync(item); } await _context.SaveChangesAsync(); return(new Response { Status = true, Message = "success" }); } }