// descriptive name for method follows IActionResult // return type is IActionResult for flexibility ***What does that allow us, again?*** public IActionResult AddPickle(Pickle pickleToAdd) { // the below if statement says if there are none in stock, make the current thing the stock; // otherwise, find the thing that exists and add the stock to this one // every class should have one thing that it does; single responsibility principle // controllers single responsibility is to take things in from the internet and return things to the internet; communication with http stuff // currently, this class both stores info AND communicates http stuff; something with two jobs means you need to break the thing up; // common interview question: one of the more common ways that people deal with storing data is with a pattern called the repository pattern; // some people love it, some hate it; it does perform the job; specifically for managing data and data storage; // INTERVIEW QUESTIONS // what's the difference between an abstract class and an interface? // what's the difference between a value type and a reference type? // can you describe the repository pattern OR can you describe a pattern that you have used in code? // Repository pattern is one of the more common in software development // patterns are more for the person reading the code than for being a software developer // the DataAccessLayer(DAL, DataAccess, Data) are the same as our data folder in React; this is a division of duty significance // if we reuse the below if/else, we can move that code to aits own method called AddOrUpdate var existingPickle = _repository.GetByType(pickleToAdd.Type); if (existingPickle == null) { var newPickle = _repository.Add(pickleToAdd); return(Created("", newPickle)); } else { var updatedPickle = _repository.Update(pickleToAdd); return(Ok(pickleToAdd)); } // created says "what is the rest url to get it; nothing yet, so here's an empty string; // when we add things, we want a 201 http response }
public IActionResult AddPickle(Pickle pickleToAdd) { var existingPickle = _repository.GetByType(pickleToAdd.Type); if (existingPickle == null) { var newPickle = _repository.Add(pickleToAdd); return(Created("", newPickle)); } else { var updatedPickle = _repository.Update(pickleToAdd); return(Ok(updatedPickle)); } }