public async Task <IHttpActionResult> SaveDropdownValue(DropdownDTO dto) { try { return(Ok(await _serv.SaveDropDownValue(dto))); } catch (Exception ex) { return(InternalServerError(ex)); } }
public async Task <int> SaveDropDownValue(DropdownDTO dto) { var returnId = 0; if (dto.Id > 0) { var existing = await _db.DropdownValue.Where(w => w.Id == dto.Id).FirstOrDefaultAsync(); if (existing != null) { existing.Active = dto.Active; existing.Value = dto.Name; existing.DropdownTypeId = dto.TypeId; await _db.SaveChangesAsync(); returnId = existing.Id; } } else { var existing = await _db.DropdownValue.FirstOrDefaultAsync(w => w.Value == dto.Name); if (existing != null) { existing.Active = dto.Active; existing.Value = dto.Name; existing.DropdownTypeId = dto.TypeId; await _db.SaveChangesAsync(); returnId = existing.Id; } else { var newDDVal = new DropdownValue { Active = dto.Active, Value = dto.Name, DropdownTypeId = dto.TypeId }; _db.DropdownValue.Add(newDDVal); await _db.SaveChangesAsync(); returnId = newDDVal.Id; } } return(returnId); }