Exemplo n.º 1
0
        public async Task <Studio> UpdateAsync(int id, UpdateStudioDto studioToUpdate)
        {
            if (id < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(id), id, "Id cannot be lower than 1.");
            }

            if (studioToUpdate == null)
            {
                throw new ArgumentNullException(nameof(studioToUpdate));
            }

            if (studioToUpdate.Name.Length > 35)
            {
                throw new ArgumentOutOfRangeException(nameof(studioToUpdate.Name), studioToUpdate.Name, "Studio name length cannot be greater than 35.");
            }

            if (!await _studiosRepository.ExistsById(id))
            {
                throw new DataNotFoundException($"Studio Id:{id} doesn't exists.");
            }

            if (await _studiosRepository.ExistsByName(studioToUpdate.Name))
            {
                throw new ArgumentException(nameof(studioToUpdate.Name), $"Studio {studioToUpdate.Name} already exists.");
            }

            return(await _studiosRepository.UpdateAsync(id, studioToUpdate));
        }
Exemplo n.º 2
0
        public async Task <Studio> UpdateAsync(int id, UpdateStudioDto studioToUpdate)
        {
            var studio = await _context.Studios.FirstAsync(c => c.Id == id);

            studio.Name          = studioToUpdate.Name;
            studio.Country       = studioToUpdate.Country;
            studio.Creation_date = studioToUpdate.Creation_date;
            await _context.SaveChangesAsync();

            return(studio);
        }
Exemplo n.º 3
0
 public async Task <IActionResult> UpdateAsync([FromRoute] int id, [FromBody] UpdateStudioDto model)
 {
     try
     {
         return(Ok(await _studiosService.UpdateAsync(id, model)));
     }
     catch (DataNotFoundException ex)
     {
         return(NotFound(ex.Message));
     }
     catch (ArgumentException ex)
     {
         return(BadRequest(ex.Message));
     }
 }