public async Task <IActionResult> Update(int?id, [FromBody] RespondentDto dto)
        {
            _logger.LogInformation("Performing update request...");

            if (Util.IsAnyNullOrEmpty(dto) || id == null)
            {
                _logger.LogWarning("Body from request is empty.");
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(new UnprocessableEntityObjectResult(ModelState));
            }

            _logger.LogInformation("Updating record...");

            var response = await _respondentService.UpdateRespondent(id.Value, dto);

            if (response.Equals(ServiceResponseType.NotFound))
            {
                _logger.LogWarning("Record does not exist. Unable to update record.");
                return(NotFound());
            }

            return(Ok());
        }
Example #2
0
        public async Task AddRespondent(RespondentDto dto)
        {
            var respondent = _mapper.Map <Respondent>(dto);

            await _respondentRepository.AddAsync(respondent);

            dto.Id = respondent.Id;
        }
Example #3
0
        /// <summary>
        /// Adds new respondent to database
        /// </summary>
        /// <param name="newEntry">
        /// The new entry.
        /// </param>
        /// <returns>
        /// The <see cref="CommonResultGeneric{T}"/>.
        /// </returns>
        public CommonResultGeneric <RespondentDto> Add(RespondentDto newEntry)
        {
            var respondent = this.respondentsRepository.Add(newEntry);

            if (respondent.Id == 0)
            {
                return(CommonResultGeneric <RespondentDto> .Failure <RespondentDto>(
                           "Some problems occured while adding new rewspondent"));
            }

            return(CommonResultGeneric <RespondentDto> .Success(respondent));
        }
Example #4
0
        public async Task <ServiceResponseType> UpdateRespondent(int id, RespondentDto dto)
        {
            var respondent = await _respondentRepository.GetAsync(id);

            if (respondent == null)
            {
                return(ServiceResponseType.NotFound);
            }

            respondent.Name           = dto.Name ?? respondent.Name;
            respondent.HashedPassword = dto.Email ?? respondent.HashedPassword;
            respondent.Email          = dto.Email ?? respondent.Email;

            await _respondentRepository.Edit(respondent);

            return(ServiceResponseType.Ok);
        }
        /// <summary>
        /// The add.
        /// </summary>
        /// <param name="newEntry">
        /// The new entry.
        /// </param>
        /// <returns>
        /// The <see cref="RespondentDto"/>.
        /// </returns>
        public RespondentDto Add(RespondentDto newEntry)
        {
            var newRepondent = this.mapper.Map <Respondent>(newEntry);

            this.context.Respondents.Add(newRepondent);
            this.context.SaveChanges();
            newEntry.Id = newRepondent.Id;

            var programmingLanguages = this.context.Experiances.Join(
                newEntry.ProgrammingLanguagesString.Split(','),
                pl => pl.Name,
                pls => pls,
                (pl, pls) => new ProgrammingLanguagesDto {
                ExperianceId = pl.Id, RespondentId = newRepondent.Id
            })
                                       .ToList();

            var framewors = this.context.Experiances.Join(
                newEntry.FrameworksString.Split(','),
                pl => pl.Name,
                pls => pls,
                (pl, pls) => new FrameworksDto {
                ExperianceId = pl.Id, RespondentId = newRepondent.Id
            })
                            .ToList();

            var databases = this.context.Experiances.Join(
                newEntry.DatabasesString.Split(','),
                pl => pl.Name,
                pls => pls,
                (pl, pls) => new DatabasesDto {
                ExperianceId = pl.Id, RespondentId = newRepondent.Id
            })
                            .ToList();

            this.context.ProgrammingLanguages.AddRange(
                this.mapper.Map <List <ProgrammingLanguages> >(programmingLanguages));
            this.context.Frameworks.AddRange(
                this.mapper.Map <List <Frameworks> >(framewors));
            this.context.Databases.AddRange(
                this.mapper.Map <List <Databases> >(databases));

            this.context.SaveChanges();
            return(newEntry);
        }
        public async Task <IActionResult> Add([FromBody] RespondentDto dto)
        {
            _logger.LogInformation("Performing insert request...");

            if (Util.IsAnyNullOrEmpty(dto))
            {
                _logger.LogWarning("Body from request is empty.");
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(new UnprocessableEntityObjectResult(ModelState));
            }

            _logger.LogInformation("Saving record...");

            await _respondentService.AddRespondent(dto);

            _logger.LogInformation("Record saved successfully. Redirecting to record info...");

            return(RedirectToAction(nameof(GetRespondentById), new { id = dto.Id }));
        }