Last active
January 23, 2020 04:46
-
-
Save pedrovasconcellos/65881be2c93af218eee874d1b380d4e9 to your computer and use it in GitHub Desktop.
Response Model
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| /// <summary> | |
| /// Response Model | |
| /// </summary> | |
| public class ResponseModel<T> | |
| { | |
| /// <summary> | |
| /// Response Model builder | |
| /// </summary> | |
| public ResponseModel() | |
| { | |
| this.Errors = new Dictionary<string, List<string>>(); | |
| } | |
| /// <summary> | |
| /// Response Model builder | |
| /// </summary> | |
| public ResponseModel(T data) | |
| { | |
| this.Data = data; | |
| } | |
| /// <summary> | |
| /// Response Model builder | |
| /// </summary> | |
| /// <param name="errors"></param> | |
| public ResponseModel(Dictionary<string, List<string>> errors) | |
| { | |
| this.Errors = errors; | |
| } | |
| /// <summary> | |
| /// Response Model builder | |
| /// </summary> | |
| /// <param name="parameterError"></param> | |
| /// <param name="error"></param> | |
| public ResponseModel(string parameterError, string error) | |
| { | |
| this.Errors = new Dictionary<string, List<string>> | |
| { | |
| { parameterError, new List<string> { error } } | |
| }; | |
| } | |
| /// <summary> | |
| /// Data | |
| /// </summary> | |
| public T Data { get; private set; } | |
| /// <summary> | |
| /// Errors | |
| /// </summary> | |
| public Dictionary<string, List<string>> Errors { get; private set; } | |
| /// <summary> | |
| /// Add error | |
| /// </summary> | |
| /// <param name="parameter"></param> | |
| /// <param name="error"></param> | |
| /// <returns></returns> | |
| public ResponseModel<T> AddError(string parameter, string error) | |
| { | |
| if (error == null || string.IsNullOrEmpty(parameter)) | |
| return this; | |
| if (this.Errors.Keys.Contains(parameter)) | |
| { | |
| this.Errors.TryGetValue(parameter, out var errorList); | |
| errorList?.Add(error); | |
| } | |
| else | |
| Errors.Add(parameter, new List<string> { error }); | |
| return this; | |
| } | |
| /// <summary> | |
| /// Add error | |
| /// </summary> | |
| /// <param name="parameter"></param> | |
| /// <param name="errors"></param> | |
| /// <returns></returns> | |
| public ResponseModel<T> AddError(string parameter, List<string> errors) | |
| { | |
| if (errors == null || errors.Count == 0 || string.IsNullOrEmpty(parameter)) | |
| return this; | |
| if (this.Errors.Keys.Contains(parameter)) | |
| { | |
| this.Errors.TryGetValue(parameter, out var errorList); | |
| if(errorList != null) | |
| foreach (var error in errors) | |
| errorList.Add(error); | |
| } | |
| else | |
| Errors.Add(parameter, errors); | |
| return this; | |
| } | |
| /// <summary> | |
| /// Contain error description | |
| /// </summary> | |
| /// <param name="parameter"></param> | |
| /// <returns></returns> | |
| public bool ContainErrorParamter(string parameter) | |
| { | |
| return this.Errors.Keys.Contains(parameter); | |
| } | |
| /// <summary> | |
| /// Contain error description | |
| /// </summary> | |
| /// <param name="errorDescription"></param> | |
| /// <returns></returns> | |
| public bool ContainErrorDescription(string errorDescription) | |
| { | |
| return this.Errors.Values.Contains(new List<string> { errorDescription }); | |
| } | |
| /// <summary> | |
| /// Union | |
| /// </summary> | |
| /// <param name="model"></param> | |
| /// <returns></returns> | |
| public ResponseModel<T> Union(ResponseModel<T> model) | |
| { | |
| this.Errors = this.Errors.Concat(model.Errors).ToDictionary(s => s.Key, s => s.Value); | |
| return this; | |
| } | |
| /// <summary> | |
| /// Has error? | |
| /// </summary> | |
| /// <returns></returns> | |
| public bool HasError() => this.Errors != null && this.Errors.Count > 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment