public Service AddServiceDtoToService(AddServiceDto addServiceDto)
 {
     return(new Service
     {
         Name = addServiceDto.Name,
         Description = addServiceDto.Description,
         IsAvailable = addServiceDto.IsAvailable
     });
 }
        public async Task <IActionResult> Add(AddServiceDto addServiceDto)
        {
            if (!ModelState.IsValid)
            {
                return(View());
            }
            await _serviceService.AddServiceAsync(addServiceDto);

            return(RedirectToAction("list", "service"));
        }
Example #3
0
        public async Task <ServiceResponse> AddService(AddServiceDto serviceDTO)
        {
            // Check if name already exists
            var exists = await _context.Services
                         .FirstOrDefaultAsync(x => x.Name.ToLower() == serviceDTO.Name.ToLower());

            if (exists != null)
            {
                throw new Exception("Conflict");
            }

            var newService = new Models.Service
            {
                Name        = serviceDTO.Name,
                Description = serviceDTO.Description,
            };
            await _context.Services.AddAsync(newService);

            await _context.SaveChangesAsync();

            return(newService.ToDTO());
        }
        public async Task <IActionResult> AddService(AddServiceDto serviceDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            try
            {
                var service = await _serviceService.AddService(serviceDto);

                return(CreatedAtAction(nameof(GetByID), new { service.Id }, service));
            }
            catch (Exception ex)
            {
                if (ex.Message.ToLower() == "conflict")
                {
                    return(Conflict($"Service with the name: {serviceDto.Name} already exists."));
                }

                return(StatusCode(500));
            }
        }
Example #5
0
 public async Task <int> AddServiceAsync(AddServiceDto addServiceDto)
 {
     return(await _repository.AddAsync(_converter.AddServiceDtoToService(addServiceDto)));
 }
        async private void saveService(object sender, EventArgs e)
        {
            if (spinner.IsVisible)
            {
                return;
            }

            ServicenameError.Text = "";
            Imageerror.Text       = "";
            descriptionError.Text = "";

            if (string.IsNullOrEmpty(Servicename.Text.Trim()))
            {
                ServicenameError.Text = "Name is required";
                return;
            }

            if (string.IsNullOrEmpty(description.Text.Trim()))
            {
                descriptionError.Text = "Description is required";
                return;
            }

            if (Image == null)
            {
                Imageerror.Text = "Image is required";
                return;
            }

            if (!dropdown.IsValidated)
            {
                return;
            }

            var serverdto = new AddServiceDto()
            {
                ParentID    = CategoryID,
                image       = ReadFully(Image),
                Desc        = description.Text,
                Name        = Servicename.Text,
                FullName    = Application.Current.Properties["FullName"].ToString(),
                phoneNumber = Application.Current.Properties["PhoneNumber"].ToString(),
                UserId      = Application.Current.Properties["ID"].ToString()
            };

            spinner.IsVisible = true;
            HttpResponseMessage response;

            using (var client = new HttpClient())
            {
                response = await client.PostAsync(
                    "http://172.107.175.25/umbraco/api/ServicesApi/AddService",
                    new StringContent(JsonConvert.SerializeObject(serverdto), Encoding.UTF8, "application/json"));
            }
            spinner.IsVisible = false;

            if (!response.IsSuccessStatusCode)
            {
                await DisplayAlert("Error", "Error From Server", "Close");

                image.Source = null;
            }
            else
            {
                await DisplayAlert("Done!", "Service Added", "Close");

                image.Source = null;
            }
        }