Beispiel #1
0
        public async Task <IActionResult> UpsertEquipmentType([FromBody] UpsertEquipmentTypeCmd request)
        {
            try
            {
                var result = await _service.UpsertEquipmentType(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
                }));
            }
        }
Beispiel #2
0
        public async Task <Response> UpsertEquipmentType(UpsertEquipmentTypeCmd request)
        {
            /////
            var existItem = await _context.EquipmentTypes.AnyAsync(x => x.Name == request.Name);

            if (existItem)
            {
                throw new BusinessLogicException(".این رکورد از قبل موجود می باشد");
            }
            /////
            if (!string.IsNullOrEmpty(request.Id))
            {
                var item = await _context.EquipmentTypes.SingleOrDefaultAsync(x => x.Id == request.Id);

                if (item == null)
                {
                    throw new BusinessLogicException("رکوردی یافت نشد");
                }

                item.Name = request.Name;
                _context.EquipmentTypes.Update(item);
            }
            else
            {
                var item = new EquipmentType
                {
                    Id        = Guid.NewGuid().ToString(),
                    Name      = request.Name,
                    CreatedAt = DateTime.Now
                };
                await _context.EquipmentTypes.AddAsync(item);
            }
            await _context.SaveChangesAsync();

            return(new Response
            {
                Status = true,
                Message = "success"
            });
        }